esas2r_ioctl.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_ioctl.c
  3. * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
  4. *
  5. * Copyright (c) 2001-2013 ATTO Technology, Inc.
  6. * (mailto:[email protected])
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. *
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include "esas2r.h"
  44. /*
  45. * Buffered ioctl handlers. A buffered ioctl is one which requires that we
  46. * allocate a DMA-able memory area to communicate with the firmware. In
  47. * order to prevent continually allocating and freeing consistent memory,
  48. * we will allocate a global buffer the first time we need it and re-use
  49. * it for subsequent ioctl calls that require it.
  50. */
  51. u8 *esas2r_buffered_ioctl;
  52. dma_addr_t esas2r_buffered_ioctl_addr;
  53. u32 esas2r_buffered_ioctl_size;
  54. struct pci_dev *esas2r_buffered_ioctl_pcid;
  55. static DEFINE_SEMAPHORE(buffered_ioctl_semaphore);
  56. typedef int (*BUFFERED_IOCTL_CALLBACK)(struct esas2r_adapter *,
  57. struct esas2r_request *,
  58. struct esas2r_sg_context *,
  59. void *);
  60. typedef void (*BUFFERED_IOCTL_DONE_CALLBACK)(struct esas2r_adapter *,
  61. struct esas2r_request *, void *);
  62. struct esas2r_buffered_ioctl {
  63. struct esas2r_adapter *a;
  64. void *ioctl;
  65. u32 length;
  66. u32 control_code;
  67. u32 offset;
  68. BUFFERED_IOCTL_CALLBACK
  69. callback;
  70. void *context;
  71. BUFFERED_IOCTL_DONE_CALLBACK
  72. done_callback;
  73. void *done_context;
  74. };
  75. static void complete_fm_api_req(struct esas2r_adapter *a,
  76. struct esas2r_request *rq)
  77. {
  78. a->fm_api_command_done = 1;
  79. wake_up_interruptible(&a->fm_api_waiter);
  80. }
  81. /* Callbacks for building scatter/gather lists for FM API requests */
  82. static u32 get_physaddr_fm_api(struct esas2r_sg_context *sgc, u64 *addr)
  83. {
  84. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  85. int offset = sgc->cur_offset - a->save_offset;
  86. (*addr) = a->firmware.phys + offset;
  87. return a->firmware.orig_len - offset;
  88. }
  89. static u32 get_physaddr_fm_api_header(struct esas2r_sg_context *sgc, u64 *addr)
  90. {
  91. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  92. int offset = sgc->cur_offset - a->save_offset;
  93. (*addr) = a->firmware.header_buff_phys + offset;
  94. return sizeof(struct esas2r_flash_img) - offset;
  95. }
  96. /* Handle EXPRESS_IOCTL_RW_FIRMWARE ioctl with img_type = FW_IMG_FM_API. */
  97. static void do_fm_api(struct esas2r_adapter *a, struct esas2r_flash_img *fi)
  98. {
  99. struct esas2r_request *rq;
  100. if (mutex_lock_interruptible(&a->fm_api_mutex)) {
  101. fi->status = FI_STAT_BUSY;
  102. return;
  103. }
  104. rq = esas2r_alloc_request(a);
  105. if (rq == NULL) {
  106. fi->status = FI_STAT_BUSY;
  107. goto free_sem;
  108. }
  109. if (fi == &a->firmware.header) {
  110. a->firmware.header_buff = dma_alloc_coherent(&a->pcid->dev,
  111. (size_t)sizeof(
  112. struct
  113. esas2r_flash_img),
  114. (dma_addr_t *)&a->
  115. firmware.
  116. header_buff_phys,
  117. GFP_KERNEL);
  118. if (a->firmware.header_buff == NULL) {
  119. esas2r_debug("failed to allocate header buffer!");
  120. fi->status = FI_STAT_BUSY;
  121. goto free_req;
  122. }
  123. memcpy(a->firmware.header_buff, fi,
  124. sizeof(struct esas2r_flash_img));
  125. a->save_offset = a->firmware.header_buff;
  126. a->fm_api_sgc.get_phys_addr =
  127. (PGETPHYSADDR)get_physaddr_fm_api_header;
  128. } else {
  129. a->save_offset = (u8 *)fi;
  130. a->fm_api_sgc.get_phys_addr =
  131. (PGETPHYSADDR)get_physaddr_fm_api;
  132. }
  133. rq->comp_cb = complete_fm_api_req;
  134. a->fm_api_command_done = 0;
  135. a->fm_api_sgc.cur_offset = a->save_offset;
  136. if (!esas2r_fm_api(a, (struct esas2r_flash_img *)a->save_offset, rq,
  137. &a->fm_api_sgc))
  138. goto all_done;
  139. /* Now wait around for it to complete. */
  140. while (!a->fm_api_command_done)
  141. wait_event_interruptible(a->fm_api_waiter,
  142. a->fm_api_command_done);
  143. all_done:
  144. if (fi == &a->firmware.header) {
  145. memcpy(fi, a->firmware.header_buff,
  146. sizeof(struct esas2r_flash_img));
  147. dma_free_coherent(&a->pcid->dev,
  148. (size_t)sizeof(struct esas2r_flash_img),
  149. a->firmware.header_buff,
  150. (dma_addr_t)a->firmware.header_buff_phys);
  151. }
  152. free_req:
  153. esas2r_free_request(a, (struct esas2r_request *)rq);
  154. free_sem:
  155. mutex_unlock(&a->fm_api_mutex);
  156. return;
  157. }
  158. static void complete_nvr_req(struct esas2r_adapter *a,
  159. struct esas2r_request *rq)
  160. {
  161. a->nvram_command_done = 1;
  162. wake_up_interruptible(&a->nvram_waiter);
  163. }
  164. /* Callback for building scatter/gather lists for buffered ioctls */
  165. static u32 get_physaddr_buffered_ioctl(struct esas2r_sg_context *sgc,
  166. u64 *addr)
  167. {
  168. int offset = (u8 *)sgc->cur_offset - esas2r_buffered_ioctl;
  169. (*addr) = esas2r_buffered_ioctl_addr + offset;
  170. return esas2r_buffered_ioctl_size - offset;
  171. }
  172. static void complete_buffered_ioctl_req(struct esas2r_adapter *a,
  173. struct esas2r_request *rq)
  174. {
  175. a->buffered_ioctl_done = 1;
  176. wake_up_interruptible(&a->buffered_ioctl_waiter);
  177. }
  178. static u8 handle_buffered_ioctl(struct esas2r_buffered_ioctl *bi)
  179. {
  180. struct esas2r_adapter *a = bi->a;
  181. struct esas2r_request *rq;
  182. struct esas2r_sg_context sgc;
  183. u8 result = IOCTL_SUCCESS;
  184. if (down_interruptible(&buffered_ioctl_semaphore))
  185. return IOCTL_OUT_OF_RESOURCES;
  186. /* allocate a buffer or use the existing buffer. */
  187. if (esas2r_buffered_ioctl) {
  188. if (esas2r_buffered_ioctl_size < bi->length) {
  189. /* free the too-small buffer and get a new one */
  190. dma_free_coherent(&a->pcid->dev,
  191. (size_t)esas2r_buffered_ioctl_size,
  192. esas2r_buffered_ioctl,
  193. esas2r_buffered_ioctl_addr);
  194. goto allocate_buffer;
  195. }
  196. } else {
  197. allocate_buffer:
  198. esas2r_buffered_ioctl_size = bi->length;
  199. esas2r_buffered_ioctl_pcid = a->pcid;
  200. esas2r_buffered_ioctl = dma_alloc_coherent(&a->pcid->dev,
  201. (size_t)
  202. esas2r_buffered_ioctl_size,
  203. &
  204. esas2r_buffered_ioctl_addr,
  205. GFP_KERNEL);
  206. }
  207. if (!esas2r_buffered_ioctl) {
  208. esas2r_log(ESAS2R_LOG_CRIT,
  209. "could not allocate %d bytes of consistent memory "
  210. "for a buffered ioctl!",
  211. bi->length);
  212. esas2r_debug("buffered ioctl alloc failure");
  213. result = IOCTL_OUT_OF_RESOURCES;
  214. goto exit_cleanly;
  215. }
  216. memcpy(esas2r_buffered_ioctl, bi->ioctl, bi->length);
  217. rq = esas2r_alloc_request(a);
  218. if (rq == NULL) {
  219. esas2r_log(ESAS2R_LOG_CRIT,
  220. "could not allocate an internal request");
  221. result = IOCTL_OUT_OF_RESOURCES;
  222. esas2r_debug("buffered ioctl - no requests");
  223. goto exit_cleanly;
  224. }
  225. a->buffered_ioctl_done = 0;
  226. rq->comp_cb = complete_buffered_ioctl_req;
  227. sgc.cur_offset = esas2r_buffered_ioctl + bi->offset;
  228. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_buffered_ioctl;
  229. sgc.length = esas2r_buffered_ioctl_size;
  230. if (!(*bi->callback)(a, rq, &sgc, bi->context)) {
  231. /* completed immediately, no need to wait */
  232. a->buffered_ioctl_done = 0;
  233. goto free_andexit_cleanly;
  234. }
  235. /* now wait around for it to complete. */
  236. while (!a->buffered_ioctl_done)
  237. wait_event_interruptible(a->buffered_ioctl_waiter,
  238. a->buffered_ioctl_done);
  239. free_andexit_cleanly:
  240. if (result == IOCTL_SUCCESS && bi->done_callback)
  241. (*bi->done_callback)(a, rq, bi->done_context);
  242. esas2r_free_request(a, rq);
  243. exit_cleanly:
  244. if (result == IOCTL_SUCCESS)
  245. memcpy(bi->ioctl, esas2r_buffered_ioctl, bi->length);
  246. up(&buffered_ioctl_semaphore);
  247. return result;
  248. }
  249. /* SMP ioctl support */
  250. static int smp_ioctl_callback(struct esas2r_adapter *a,
  251. struct esas2r_request *rq,
  252. struct esas2r_sg_context *sgc, void *context)
  253. {
  254. struct atto_ioctl_smp *si =
  255. (struct atto_ioctl_smp *)esas2r_buffered_ioctl;
  256. esas2r_sgc_init(sgc, a, rq, rq->vrq->ioctl.sge);
  257. esas2r_build_ioctl_req(a, rq, sgc->length, VDA_IOCTL_SMP);
  258. if (!esas2r_build_sg_list(a, rq, sgc)) {
  259. si->status = ATTO_STS_OUT_OF_RSRC;
  260. return false;
  261. }
  262. esas2r_start_request(a, rq);
  263. return true;
  264. }
  265. static u8 handle_smp_ioctl(struct esas2r_adapter *a, struct atto_ioctl_smp *si)
  266. {
  267. struct esas2r_buffered_ioctl bi;
  268. memset(&bi, 0, sizeof(bi));
  269. bi.a = a;
  270. bi.ioctl = si;
  271. bi.length = sizeof(struct atto_ioctl_smp)
  272. + le32_to_cpu(si->req_length)
  273. + le32_to_cpu(si->rsp_length);
  274. bi.offset = 0;
  275. bi.callback = smp_ioctl_callback;
  276. return handle_buffered_ioctl(&bi);
  277. }
  278. /* CSMI ioctl support */
  279. static void esas2r_csmi_ioctl_tunnel_comp_cb(struct esas2r_adapter *a,
  280. struct esas2r_request *rq)
  281. {
  282. rq->target_id = le16_to_cpu(rq->func_rsp.ioctl_rsp.csmi.target_id);
  283. rq->vrq->scsi.flags |= cpu_to_le32(rq->func_rsp.ioctl_rsp.csmi.lun);
  284. /* Now call the original completion callback. */
  285. (*rq->aux_req_cb)(a, rq);
  286. }
  287. /* Tunnel a CSMI IOCTL to the back end driver for processing. */
  288. static bool csmi_ioctl_tunnel(struct esas2r_adapter *a,
  289. union atto_ioctl_csmi *ci,
  290. struct esas2r_request *rq,
  291. struct esas2r_sg_context *sgc,
  292. u32 ctrl_code,
  293. u16 target_id)
  294. {
  295. struct atto_vda_ioctl_req *ioctl = &rq->vrq->ioctl;
  296. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  297. return false;
  298. esas2r_sgc_init(sgc, a, rq, rq->vrq->ioctl.sge);
  299. esas2r_build_ioctl_req(a, rq, sgc->length, VDA_IOCTL_CSMI);
  300. ioctl->csmi.ctrl_code = cpu_to_le32(ctrl_code);
  301. ioctl->csmi.target_id = cpu_to_le16(target_id);
  302. ioctl->csmi.lun = (u8)le32_to_cpu(rq->vrq->scsi.flags);
  303. /*
  304. * Always usurp the completion callback since the interrupt callback
  305. * mechanism may be used.
  306. */
  307. rq->aux_req_cx = ci;
  308. rq->aux_req_cb = rq->comp_cb;
  309. rq->comp_cb = esas2r_csmi_ioctl_tunnel_comp_cb;
  310. if (!esas2r_build_sg_list(a, rq, sgc))
  311. return false;
  312. esas2r_start_request(a, rq);
  313. return true;
  314. }
  315. static bool check_lun(struct scsi_lun lun)
  316. {
  317. bool result;
  318. result = ((lun.scsi_lun[7] == 0) &&
  319. (lun.scsi_lun[6] == 0) &&
  320. (lun.scsi_lun[5] == 0) &&
  321. (lun.scsi_lun[4] == 0) &&
  322. (lun.scsi_lun[3] == 0) &&
  323. (lun.scsi_lun[2] == 0) &&
  324. /* Byte 1 is intentionally skipped */
  325. (lun.scsi_lun[0] == 0));
  326. return result;
  327. }
  328. static int csmi_ioctl_callback(struct esas2r_adapter *a,
  329. struct esas2r_request *rq,
  330. struct esas2r_sg_context *sgc, void *context)
  331. {
  332. struct atto_csmi *ci = (struct atto_csmi *)context;
  333. union atto_ioctl_csmi *ioctl_csmi =
  334. (union atto_ioctl_csmi *)esas2r_buffered_ioctl;
  335. u8 path = 0;
  336. u8 tid = 0;
  337. u8 lun = 0;
  338. u32 sts = CSMI_STS_SUCCESS;
  339. struct esas2r_target *t;
  340. unsigned long flags;
  341. if (ci->control_code == CSMI_CC_GET_DEV_ADDR) {
  342. struct atto_csmi_get_dev_addr *gda = &ci->data.dev_addr;
  343. path = gda->path_id;
  344. tid = gda->target_id;
  345. lun = gda->lun;
  346. } else if (ci->control_code == CSMI_CC_TASK_MGT) {
  347. struct atto_csmi_task_mgmt *tm = &ci->data.tsk_mgt;
  348. path = tm->path_id;
  349. tid = tm->target_id;
  350. lun = tm->lun;
  351. }
  352. if (path > 0) {
  353. rq->func_rsp.ioctl_rsp.csmi.csmi_status = cpu_to_le32(
  354. CSMI_STS_INV_PARAM);
  355. return false;
  356. }
  357. rq->target_id = tid;
  358. rq->vrq->scsi.flags |= cpu_to_le32(lun);
  359. switch (ci->control_code) {
  360. case CSMI_CC_GET_DRVR_INFO:
  361. {
  362. struct atto_csmi_get_driver_info *gdi = &ioctl_csmi->drvr_info;
  363. strcpy(gdi->description, esas2r_get_model_name(a));
  364. gdi->csmi_major_rev = CSMI_MAJOR_REV;
  365. gdi->csmi_minor_rev = CSMI_MINOR_REV;
  366. break;
  367. }
  368. case CSMI_CC_GET_CNTLR_CFG:
  369. {
  370. struct atto_csmi_get_cntlr_cfg *gcc = &ioctl_csmi->cntlr_cfg;
  371. gcc->base_io_addr = 0;
  372. pci_read_config_dword(a->pcid, PCI_BASE_ADDRESS_2,
  373. &gcc->base_memaddr_lo);
  374. pci_read_config_dword(a->pcid, PCI_BASE_ADDRESS_3,
  375. &gcc->base_memaddr_hi);
  376. gcc->board_id = MAKEDWORD(a->pcid->subsystem_device,
  377. a->pcid->subsystem_vendor);
  378. gcc->slot_num = CSMI_SLOT_NUM_UNKNOWN;
  379. gcc->cntlr_class = CSMI_CNTLR_CLASS_HBA;
  380. gcc->io_bus_type = CSMI_BUS_TYPE_PCI;
  381. gcc->pci_addr.bus_num = a->pcid->bus->number;
  382. gcc->pci_addr.device_num = PCI_SLOT(a->pcid->devfn);
  383. gcc->pci_addr.function_num = PCI_FUNC(a->pcid->devfn);
  384. memset(gcc->serial_num, 0, sizeof(gcc->serial_num));
  385. gcc->major_rev = LOBYTE(LOWORD(a->fw_version));
  386. gcc->minor_rev = HIBYTE(LOWORD(a->fw_version));
  387. gcc->build_rev = LOBYTE(HIWORD(a->fw_version));
  388. gcc->release_rev = HIBYTE(HIWORD(a->fw_version));
  389. gcc->bios_major_rev = HIBYTE(HIWORD(a->flash_ver));
  390. gcc->bios_minor_rev = LOBYTE(HIWORD(a->flash_ver));
  391. gcc->bios_build_rev = LOWORD(a->flash_ver);
  392. if (test_bit(AF2_THUNDERLINK, &a->flags2))
  393. gcc->cntlr_flags = CSMI_CNTLRF_SAS_HBA
  394. | CSMI_CNTLRF_SATA_HBA;
  395. else
  396. gcc->cntlr_flags = CSMI_CNTLRF_SAS_RAID
  397. | CSMI_CNTLRF_SATA_RAID;
  398. gcc->rrom_major_rev = 0;
  399. gcc->rrom_minor_rev = 0;
  400. gcc->rrom_build_rev = 0;
  401. gcc->rrom_release_rev = 0;
  402. gcc->rrom_biosmajor_rev = 0;
  403. gcc->rrom_biosminor_rev = 0;
  404. gcc->rrom_biosbuild_rev = 0;
  405. gcc->rrom_biosrelease_rev = 0;
  406. break;
  407. }
  408. case CSMI_CC_GET_CNTLR_STS:
  409. {
  410. struct atto_csmi_get_cntlr_sts *gcs = &ioctl_csmi->cntlr_sts;
  411. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  412. gcs->status = CSMI_CNTLR_STS_FAILED;
  413. else
  414. gcs->status = CSMI_CNTLR_STS_GOOD;
  415. gcs->offline_reason = CSMI_OFFLINE_NO_REASON;
  416. break;
  417. }
  418. case CSMI_CC_FW_DOWNLOAD:
  419. case CSMI_CC_GET_RAID_INFO:
  420. case CSMI_CC_GET_RAID_CFG:
  421. sts = CSMI_STS_BAD_CTRL_CODE;
  422. break;
  423. case CSMI_CC_SMP_PASSTHRU:
  424. case CSMI_CC_SSP_PASSTHRU:
  425. case CSMI_CC_STP_PASSTHRU:
  426. case CSMI_CC_GET_PHY_INFO:
  427. case CSMI_CC_SET_PHY_INFO:
  428. case CSMI_CC_GET_LINK_ERRORS:
  429. case CSMI_CC_GET_SATA_SIG:
  430. case CSMI_CC_GET_CONN_INFO:
  431. case CSMI_CC_PHY_CTRL:
  432. if (!csmi_ioctl_tunnel(a, ioctl_csmi, rq, sgc,
  433. ci->control_code,
  434. ESAS2R_TARG_ID_INV)) {
  435. sts = CSMI_STS_FAILED;
  436. break;
  437. }
  438. return true;
  439. case CSMI_CC_GET_SCSI_ADDR:
  440. {
  441. struct atto_csmi_get_scsi_addr *gsa = &ioctl_csmi->scsi_addr;
  442. struct scsi_lun lun;
  443. memcpy(&lun, gsa->sas_lun, sizeof(struct scsi_lun));
  444. if (!check_lun(lun)) {
  445. sts = CSMI_STS_NO_SCSI_ADDR;
  446. break;
  447. }
  448. /* make sure the device is present */
  449. spin_lock_irqsave(&a->mem_lock, flags);
  450. t = esas2r_targ_db_find_by_sas_addr(a, (u64 *)gsa->sas_addr);
  451. spin_unlock_irqrestore(&a->mem_lock, flags);
  452. if (t == NULL) {
  453. sts = CSMI_STS_NO_SCSI_ADDR;
  454. break;
  455. }
  456. gsa->host_index = 0xFF;
  457. gsa->lun = gsa->sas_lun[1];
  458. rq->target_id = esas2r_targ_get_id(t, a);
  459. break;
  460. }
  461. case CSMI_CC_GET_DEV_ADDR:
  462. {
  463. struct atto_csmi_get_dev_addr *gda = &ioctl_csmi->dev_addr;
  464. /* make sure the target is present */
  465. t = a->targetdb + rq->target_id;
  466. if (t >= a->targetdb_end
  467. || t->target_state != TS_PRESENT
  468. || t->sas_addr == 0) {
  469. sts = CSMI_STS_NO_DEV_ADDR;
  470. break;
  471. }
  472. /* fill in the result */
  473. *(u64 *)gda->sas_addr = t->sas_addr;
  474. memset(gda->sas_lun, 0, sizeof(gda->sas_lun));
  475. gda->sas_lun[1] = (u8)le32_to_cpu(rq->vrq->scsi.flags);
  476. break;
  477. }
  478. case CSMI_CC_TASK_MGT:
  479. /* make sure the target is present */
  480. t = a->targetdb + rq->target_id;
  481. if (t >= a->targetdb_end
  482. || t->target_state != TS_PRESENT
  483. || !(t->flags & TF_PASS_THRU)) {
  484. sts = CSMI_STS_NO_DEV_ADDR;
  485. break;
  486. }
  487. if (!csmi_ioctl_tunnel(a, ioctl_csmi, rq, sgc,
  488. ci->control_code,
  489. t->phys_targ_id)) {
  490. sts = CSMI_STS_FAILED;
  491. break;
  492. }
  493. return true;
  494. default:
  495. sts = CSMI_STS_BAD_CTRL_CODE;
  496. break;
  497. }
  498. rq->func_rsp.ioctl_rsp.csmi.csmi_status = cpu_to_le32(sts);
  499. return false;
  500. }
  501. static void csmi_ioctl_done_callback(struct esas2r_adapter *a,
  502. struct esas2r_request *rq, void *context)
  503. {
  504. struct atto_csmi *ci = (struct atto_csmi *)context;
  505. union atto_ioctl_csmi *ioctl_csmi =
  506. (union atto_ioctl_csmi *)esas2r_buffered_ioctl;
  507. switch (ci->control_code) {
  508. case CSMI_CC_GET_DRVR_INFO:
  509. {
  510. struct atto_csmi_get_driver_info *gdi =
  511. &ioctl_csmi->drvr_info;
  512. strcpy(gdi->name, ESAS2R_VERSION_STR);
  513. gdi->major_rev = ESAS2R_MAJOR_REV;
  514. gdi->minor_rev = ESAS2R_MINOR_REV;
  515. gdi->build_rev = 0;
  516. gdi->release_rev = 0;
  517. break;
  518. }
  519. case CSMI_CC_GET_SCSI_ADDR:
  520. {
  521. struct atto_csmi_get_scsi_addr *gsa = &ioctl_csmi->scsi_addr;
  522. if (le32_to_cpu(rq->func_rsp.ioctl_rsp.csmi.csmi_status) ==
  523. CSMI_STS_SUCCESS) {
  524. gsa->target_id = rq->target_id;
  525. gsa->path_id = 0;
  526. }
  527. break;
  528. }
  529. }
  530. ci->status = le32_to_cpu(rq->func_rsp.ioctl_rsp.csmi.csmi_status);
  531. }
  532. static u8 handle_csmi_ioctl(struct esas2r_adapter *a, struct atto_csmi *ci)
  533. {
  534. struct esas2r_buffered_ioctl bi;
  535. memset(&bi, 0, sizeof(bi));
  536. bi.a = a;
  537. bi.ioctl = &ci->data;
  538. bi.length = sizeof(union atto_ioctl_csmi);
  539. bi.offset = 0;
  540. bi.callback = csmi_ioctl_callback;
  541. bi.context = ci;
  542. bi.done_callback = csmi_ioctl_done_callback;
  543. bi.done_context = ci;
  544. return handle_buffered_ioctl(&bi);
  545. }
  546. /* ATTO HBA ioctl support */
  547. /* Tunnel an ATTO HBA IOCTL to the back end driver for processing. */
  548. static bool hba_ioctl_tunnel(struct esas2r_adapter *a,
  549. struct atto_ioctl *hi,
  550. struct esas2r_request *rq,
  551. struct esas2r_sg_context *sgc)
  552. {
  553. esas2r_sgc_init(sgc, a, rq, rq->vrq->ioctl.sge);
  554. esas2r_build_ioctl_req(a, rq, sgc->length, VDA_IOCTL_HBA);
  555. if (!esas2r_build_sg_list(a, rq, sgc)) {
  556. hi->status = ATTO_STS_OUT_OF_RSRC;
  557. return false;
  558. }
  559. esas2r_start_request(a, rq);
  560. return true;
  561. }
  562. static void scsi_passthru_comp_cb(struct esas2r_adapter *a,
  563. struct esas2r_request *rq)
  564. {
  565. struct atto_ioctl *hi = (struct atto_ioctl *)rq->aux_req_cx;
  566. struct atto_hba_scsi_pass_thru *spt = &hi->data.scsi_pass_thru;
  567. u8 sts = ATTO_SPT_RS_FAILED;
  568. spt->scsi_status = rq->func_rsp.scsi_rsp.scsi_stat;
  569. spt->sense_length = rq->sense_len;
  570. spt->residual_length =
  571. le32_to_cpu(rq->func_rsp.scsi_rsp.residual_length);
  572. switch (rq->req_stat) {
  573. case RS_SUCCESS:
  574. case RS_SCSI_ERROR:
  575. sts = ATTO_SPT_RS_SUCCESS;
  576. break;
  577. case RS_UNDERRUN:
  578. sts = ATTO_SPT_RS_UNDERRUN;
  579. break;
  580. case RS_OVERRUN:
  581. sts = ATTO_SPT_RS_OVERRUN;
  582. break;
  583. case RS_SEL:
  584. case RS_SEL2:
  585. sts = ATTO_SPT_RS_NO_DEVICE;
  586. break;
  587. case RS_NO_LUN:
  588. sts = ATTO_SPT_RS_NO_LUN;
  589. break;
  590. case RS_TIMEOUT:
  591. sts = ATTO_SPT_RS_TIMEOUT;
  592. break;
  593. case RS_DEGRADED:
  594. sts = ATTO_SPT_RS_DEGRADED;
  595. break;
  596. case RS_BUSY:
  597. sts = ATTO_SPT_RS_BUSY;
  598. break;
  599. case RS_ABORTED:
  600. sts = ATTO_SPT_RS_ABORTED;
  601. break;
  602. case RS_RESET:
  603. sts = ATTO_SPT_RS_BUS_RESET;
  604. break;
  605. }
  606. spt->req_status = sts;
  607. /* Update the target ID to the next one present. */
  608. spt->target_id =
  609. esas2r_targ_db_find_next_present(a, (u16)spt->target_id);
  610. /* Done, call the completion callback. */
  611. (*rq->aux_req_cb)(a, rq);
  612. }
  613. static int hba_ioctl_callback(struct esas2r_adapter *a,
  614. struct esas2r_request *rq,
  615. struct esas2r_sg_context *sgc,
  616. void *context)
  617. {
  618. struct atto_ioctl *hi = (struct atto_ioctl *)esas2r_buffered_ioctl;
  619. hi->status = ATTO_STS_SUCCESS;
  620. switch (hi->function) {
  621. case ATTO_FUNC_GET_ADAP_INFO:
  622. {
  623. u8 *class_code = (u8 *)&a->pcid->class;
  624. struct atto_hba_get_adapter_info *gai =
  625. &hi->data.get_adap_info;
  626. if (hi->flags & HBAF_TUNNEL) {
  627. hi->status = ATTO_STS_UNSUPPORTED;
  628. break;
  629. }
  630. if (hi->version > ATTO_VER_GET_ADAP_INFO0) {
  631. hi->status = ATTO_STS_INV_VERSION;
  632. hi->version = ATTO_VER_GET_ADAP_INFO0;
  633. break;
  634. }
  635. memset(gai, 0, sizeof(*gai));
  636. gai->pci.vendor_id = a->pcid->vendor;
  637. gai->pci.device_id = a->pcid->device;
  638. gai->pci.ss_vendor_id = a->pcid->subsystem_vendor;
  639. gai->pci.ss_device_id = a->pcid->subsystem_device;
  640. gai->pci.class_code[0] = class_code[0];
  641. gai->pci.class_code[1] = class_code[1];
  642. gai->pci.class_code[2] = class_code[2];
  643. gai->pci.rev_id = a->pcid->revision;
  644. gai->pci.bus_num = a->pcid->bus->number;
  645. gai->pci.dev_num = PCI_SLOT(a->pcid->devfn);
  646. gai->pci.func_num = PCI_FUNC(a->pcid->devfn);
  647. if (pci_is_pcie(a->pcid)) {
  648. u16 stat;
  649. u32 caps;
  650. pcie_capability_read_word(a->pcid, PCI_EXP_LNKSTA,
  651. &stat);
  652. pcie_capability_read_dword(a->pcid, PCI_EXP_LNKCAP,
  653. &caps);
  654. gai->pci.link_speed_curr =
  655. (u8)(stat & PCI_EXP_LNKSTA_CLS);
  656. gai->pci.link_speed_max =
  657. (u8)(caps & PCI_EXP_LNKCAP_SLS);
  658. gai->pci.link_width_curr =
  659. (u8)((stat & PCI_EXP_LNKSTA_NLW)
  660. >> PCI_EXP_LNKSTA_NLW_SHIFT);
  661. gai->pci.link_width_max =
  662. (u8)((caps & PCI_EXP_LNKCAP_MLW)
  663. >> 4);
  664. }
  665. gai->pci.msi_vector_cnt = 1;
  666. if (a->pcid->msix_enabled)
  667. gai->pci.interrupt_mode = ATTO_GAI_PCIIM_MSIX;
  668. else if (a->pcid->msi_enabled)
  669. gai->pci.interrupt_mode = ATTO_GAI_PCIIM_MSI;
  670. else
  671. gai->pci.interrupt_mode = ATTO_GAI_PCIIM_LEGACY;
  672. gai->adap_type = ATTO_GAI_AT_ESASRAID2;
  673. if (test_bit(AF2_THUNDERLINK, &a->flags2))
  674. gai->adap_type = ATTO_GAI_AT_TLSASHBA;
  675. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  676. gai->adap_flags |= ATTO_GAI_AF_DEGRADED;
  677. gai->adap_flags |= ATTO_GAI_AF_SPT_SUPP |
  678. ATTO_GAI_AF_DEVADDR_SUPP;
  679. if (a->pcid->subsystem_device == ATTO_ESAS_R60F
  680. || a->pcid->subsystem_device == ATTO_ESAS_R608
  681. || a->pcid->subsystem_device == ATTO_ESAS_R644
  682. || a->pcid->subsystem_device == ATTO_TSSC_3808E)
  683. gai->adap_flags |= ATTO_GAI_AF_VIRT_SES;
  684. gai->num_ports = ESAS2R_NUM_PHYS;
  685. gai->num_phys = ESAS2R_NUM_PHYS;
  686. strcpy(gai->firmware_rev, a->fw_rev);
  687. strcpy(gai->flash_rev, a->flash_rev);
  688. strcpy(gai->model_name_short, esas2r_get_model_name_short(a));
  689. strcpy(gai->model_name, esas2r_get_model_name(a));
  690. gai->num_targets = ESAS2R_MAX_TARGETS;
  691. gai->num_busses = 1;
  692. gai->num_targsper_bus = gai->num_targets;
  693. gai->num_lunsper_targ = 256;
  694. if (a->pcid->subsystem_device == ATTO_ESAS_R6F0
  695. || a->pcid->subsystem_device == ATTO_ESAS_R60F)
  696. gai->num_connectors = 4;
  697. else
  698. gai->num_connectors = 2;
  699. gai->adap_flags2 |= ATTO_GAI_AF2_ADAP_CTRL_SUPP;
  700. gai->num_targets_backend = a->num_targets_backend;
  701. gai->tunnel_flags = a->ioctl_tunnel
  702. & (ATTO_GAI_TF_MEM_RW
  703. | ATTO_GAI_TF_TRACE
  704. | ATTO_GAI_TF_SCSI_PASS_THRU
  705. | ATTO_GAI_TF_GET_DEV_ADDR
  706. | ATTO_GAI_TF_PHY_CTRL
  707. | ATTO_GAI_TF_CONN_CTRL
  708. | ATTO_GAI_TF_GET_DEV_INFO);
  709. break;
  710. }
  711. case ATTO_FUNC_GET_ADAP_ADDR:
  712. {
  713. struct atto_hba_get_adapter_address *gaa =
  714. &hi->data.get_adap_addr;
  715. if (hi->flags & HBAF_TUNNEL) {
  716. hi->status = ATTO_STS_UNSUPPORTED;
  717. break;
  718. }
  719. if (hi->version > ATTO_VER_GET_ADAP_ADDR0) {
  720. hi->status = ATTO_STS_INV_VERSION;
  721. hi->version = ATTO_VER_GET_ADAP_ADDR0;
  722. } else if (gaa->addr_type == ATTO_GAA_AT_PORT
  723. || gaa->addr_type == ATTO_GAA_AT_NODE) {
  724. if (gaa->addr_type == ATTO_GAA_AT_PORT
  725. && gaa->port_id >= ESAS2R_NUM_PHYS) {
  726. hi->status = ATTO_STS_NOT_APPL;
  727. } else {
  728. memcpy((u64 *)gaa->address,
  729. &a->nvram->sas_addr[0], sizeof(u64));
  730. gaa->addr_len = sizeof(u64);
  731. }
  732. } else {
  733. hi->status = ATTO_STS_INV_PARAM;
  734. }
  735. break;
  736. }
  737. case ATTO_FUNC_MEM_RW:
  738. {
  739. if (hi->flags & HBAF_TUNNEL) {
  740. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  741. return true;
  742. break;
  743. }
  744. hi->status = ATTO_STS_UNSUPPORTED;
  745. break;
  746. }
  747. case ATTO_FUNC_TRACE:
  748. {
  749. struct atto_hba_trace *trc = &hi->data.trace;
  750. if (hi->flags & HBAF_TUNNEL) {
  751. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  752. return true;
  753. break;
  754. }
  755. if (hi->version > ATTO_VER_TRACE1) {
  756. hi->status = ATTO_STS_INV_VERSION;
  757. hi->version = ATTO_VER_TRACE1;
  758. break;
  759. }
  760. if (trc->trace_type == ATTO_TRC_TT_FWCOREDUMP
  761. && hi->version >= ATTO_VER_TRACE1) {
  762. if (trc->trace_func == ATTO_TRC_TF_UPLOAD) {
  763. u32 len = hi->data_length;
  764. u32 offset = trc->current_offset;
  765. u32 total_len = ESAS2R_FWCOREDUMP_SZ;
  766. /* Size is zero if a core dump isn't present */
  767. if (!test_bit(AF2_COREDUMP_SAVED, &a->flags2))
  768. total_len = 0;
  769. if (len > total_len)
  770. len = total_len;
  771. if (offset >= total_len
  772. || offset + len > total_len
  773. || len == 0) {
  774. hi->status = ATTO_STS_INV_PARAM;
  775. break;
  776. }
  777. memcpy(trc->contents,
  778. a->fw_coredump_buff + offset,
  779. len);
  780. hi->data_length = len;
  781. } else if (trc->trace_func == ATTO_TRC_TF_RESET) {
  782. memset(a->fw_coredump_buff, 0,
  783. ESAS2R_FWCOREDUMP_SZ);
  784. clear_bit(AF2_COREDUMP_SAVED, &a->flags2);
  785. } else if (trc->trace_func != ATTO_TRC_TF_GET_INFO) {
  786. hi->status = ATTO_STS_UNSUPPORTED;
  787. break;
  788. }
  789. /* Always return all the info we can. */
  790. trc->trace_mask = 0;
  791. trc->current_offset = 0;
  792. trc->total_length = ESAS2R_FWCOREDUMP_SZ;
  793. /* Return zero length buffer if core dump not present */
  794. if (!test_bit(AF2_COREDUMP_SAVED, &a->flags2))
  795. trc->total_length = 0;
  796. } else {
  797. hi->status = ATTO_STS_UNSUPPORTED;
  798. }
  799. break;
  800. }
  801. case ATTO_FUNC_SCSI_PASS_THRU:
  802. {
  803. struct atto_hba_scsi_pass_thru *spt = &hi->data.scsi_pass_thru;
  804. struct scsi_lun lun;
  805. memcpy(&lun, spt->lun, sizeof(struct scsi_lun));
  806. if (hi->flags & HBAF_TUNNEL) {
  807. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  808. return true;
  809. break;
  810. }
  811. if (hi->version > ATTO_VER_SCSI_PASS_THRU0) {
  812. hi->status = ATTO_STS_INV_VERSION;
  813. hi->version = ATTO_VER_SCSI_PASS_THRU0;
  814. break;
  815. }
  816. if (spt->target_id >= ESAS2R_MAX_TARGETS || !check_lun(lun)) {
  817. hi->status = ATTO_STS_INV_PARAM;
  818. break;
  819. }
  820. esas2r_sgc_init(sgc, a, rq, NULL);
  821. sgc->length = hi->data_length;
  822. sgc->cur_offset += offsetof(struct atto_ioctl, data.byte)
  823. + sizeof(struct atto_hba_scsi_pass_thru);
  824. /* Finish request initialization */
  825. rq->target_id = (u16)spt->target_id;
  826. rq->vrq->scsi.flags |= cpu_to_le32(spt->lun[1]);
  827. memcpy(rq->vrq->scsi.cdb, spt->cdb, 16);
  828. rq->vrq->scsi.length = cpu_to_le32(hi->data_length);
  829. rq->sense_len = spt->sense_length;
  830. rq->sense_buf = (u8 *)spt->sense_data;
  831. /* NOTE: we ignore spt->timeout */
  832. /*
  833. * always usurp the completion callback since the interrupt
  834. * callback mechanism may be used.
  835. */
  836. rq->aux_req_cx = hi;
  837. rq->aux_req_cb = rq->comp_cb;
  838. rq->comp_cb = scsi_passthru_comp_cb;
  839. if (spt->flags & ATTO_SPTF_DATA_IN) {
  840. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
  841. } else if (spt->flags & ATTO_SPTF_DATA_OUT) {
  842. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
  843. } else {
  844. if (sgc->length) {
  845. hi->status = ATTO_STS_INV_PARAM;
  846. break;
  847. }
  848. }
  849. if (spt->flags & ATTO_SPTF_ORDERED_Q)
  850. rq->vrq->scsi.flags |=
  851. cpu_to_le32(FCP_CMND_TA_ORDRD_Q);
  852. else if (spt->flags & ATTO_SPTF_HEAD_OF_Q)
  853. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_TA_HEAD_Q);
  854. if (!esas2r_build_sg_list(a, rq, sgc)) {
  855. hi->status = ATTO_STS_OUT_OF_RSRC;
  856. break;
  857. }
  858. esas2r_start_request(a, rq);
  859. return true;
  860. }
  861. case ATTO_FUNC_GET_DEV_ADDR:
  862. {
  863. struct atto_hba_get_device_address *gda =
  864. &hi->data.get_dev_addr;
  865. struct esas2r_target *t;
  866. if (hi->flags & HBAF_TUNNEL) {
  867. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  868. return true;
  869. break;
  870. }
  871. if (hi->version > ATTO_VER_GET_DEV_ADDR0) {
  872. hi->status = ATTO_STS_INV_VERSION;
  873. hi->version = ATTO_VER_GET_DEV_ADDR0;
  874. break;
  875. }
  876. if (gda->target_id >= ESAS2R_MAX_TARGETS) {
  877. hi->status = ATTO_STS_INV_PARAM;
  878. break;
  879. }
  880. t = a->targetdb + (u16)gda->target_id;
  881. if (t->target_state != TS_PRESENT) {
  882. hi->status = ATTO_STS_FAILED;
  883. } else if (gda->addr_type == ATTO_GDA_AT_PORT) {
  884. if (t->sas_addr == 0) {
  885. hi->status = ATTO_STS_UNSUPPORTED;
  886. } else {
  887. *(u64 *)gda->address = t->sas_addr;
  888. gda->addr_len = sizeof(u64);
  889. }
  890. } else if (gda->addr_type == ATTO_GDA_AT_NODE) {
  891. hi->status = ATTO_STS_NOT_APPL;
  892. } else {
  893. hi->status = ATTO_STS_INV_PARAM;
  894. }
  895. /* update the target ID to the next one present. */
  896. gda->target_id =
  897. esas2r_targ_db_find_next_present(a,
  898. (u16)gda->target_id);
  899. break;
  900. }
  901. case ATTO_FUNC_PHY_CTRL:
  902. case ATTO_FUNC_CONN_CTRL:
  903. {
  904. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  905. return true;
  906. break;
  907. }
  908. case ATTO_FUNC_ADAP_CTRL:
  909. {
  910. struct atto_hba_adap_ctrl *ac = &hi->data.adap_ctrl;
  911. if (hi->flags & HBAF_TUNNEL) {
  912. hi->status = ATTO_STS_UNSUPPORTED;
  913. break;
  914. }
  915. if (hi->version > ATTO_VER_ADAP_CTRL0) {
  916. hi->status = ATTO_STS_INV_VERSION;
  917. hi->version = ATTO_VER_ADAP_CTRL0;
  918. break;
  919. }
  920. if (ac->adap_func == ATTO_AC_AF_HARD_RST) {
  921. esas2r_reset_adapter(a);
  922. } else if (ac->adap_func != ATTO_AC_AF_GET_STATE) {
  923. hi->status = ATTO_STS_UNSUPPORTED;
  924. break;
  925. }
  926. if (test_bit(AF_CHPRST_NEEDED, &a->flags))
  927. ac->adap_state = ATTO_AC_AS_RST_SCHED;
  928. else if (test_bit(AF_CHPRST_PENDING, &a->flags))
  929. ac->adap_state = ATTO_AC_AS_RST_IN_PROG;
  930. else if (test_bit(AF_DISC_PENDING, &a->flags))
  931. ac->adap_state = ATTO_AC_AS_RST_DISC;
  932. else if (test_bit(AF_DISABLED, &a->flags))
  933. ac->adap_state = ATTO_AC_AS_DISABLED;
  934. else if (test_bit(AF_DEGRADED_MODE, &a->flags))
  935. ac->adap_state = ATTO_AC_AS_DEGRADED;
  936. else
  937. ac->adap_state = ATTO_AC_AS_OK;
  938. break;
  939. }
  940. case ATTO_FUNC_GET_DEV_INFO:
  941. {
  942. struct atto_hba_get_device_info *gdi = &hi->data.get_dev_info;
  943. struct esas2r_target *t;
  944. if (hi->flags & HBAF_TUNNEL) {
  945. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  946. return true;
  947. break;
  948. }
  949. if (hi->version > ATTO_VER_GET_DEV_INFO0) {
  950. hi->status = ATTO_STS_INV_VERSION;
  951. hi->version = ATTO_VER_GET_DEV_INFO0;
  952. break;
  953. }
  954. if (gdi->target_id >= ESAS2R_MAX_TARGETS) {
  955. hi->status = ATTO_STS_INV_PARAM;
  956. break;
  957. }
  958. t = a->targetdb + (u16)gdi->target_id;
  959. /* update the target ID to the next one present. */
  960. gdi->target_id =
  961. esas2r_targ_db_find_next_present(a,
  962. (u16)gdi->target_id);
  963. if (t->target_state != TS_PRESENT) {
  964. hi->status = ATTO_STS_FAILED;
  965. break;
  966. }
  967. hi->status = ATTO_STS_UNSUPPORTED;
  968. break;
  969. }
  970. default:
  971. hi->status = ATTO_STS_INV_FUNC;
  972. break;
  973. }
  974. return false;
  975. }
  976. static void hba_ioctl_done_callback(struct esas2r_adapter *a,
  977. struct esas2r_request *rq, void *context)
  978. {
  979. struct atto_ioctl *ioctl_hba =
  980. (struct atto_ioctl *)esas2r_buffered_ioctl;
  981. esas2r_debug("hba_ioctl_done_callback %d", a->index);
  982. if (ioctl_hba->function == ATTO_FUNC_GET_ADAP_INFO) {
  983. struct atto_hba_get_adapter_info *gai =
  984. &ioctl_hba->data.get_adap_info;
  985. esas2r_debug("ATTO_FUNC_GET_ADAP_INFO");
  986. gai->drvr_rev_major = ESAS2R_MAJOR_REV;
  987. gai->drvr_rev_minor = ESAS2R_MINOR_REV;
  988. strcpy(gai->drvr_rev_ascii, ESAS2R_VERSION_STR);
  989. strcpy(gai->drvr_name, ESAS2R_DRVR_NAME);
  990. gai->num_busses = 1;
  991. gai->num_targsper_bus = ESAS2R_MAX_ID + 1;
  992. gai->num_lunsper_targ = 1;
  993. }
  994. }
  995. u8 handle_hba_ioctl(struct esas2r_adapter *a,
  996. struct atto_ioctl *ioctl_hba)
  997. {
  998. struct esas2r_buffered_ioctl bi;
  999. memset(&bi, 0, sizeof(bi));
  1000. bi.a = a;
  1001. bi.ioctl = ioctl_hba;
  1002. bi.length = sizeof(struct atto_ioctl) + ioctl_hba->data_length;
  1003. bi.callback = hba_ioctl_callback;
  1004. bi.context = NULL;
  1005. bi.done_callback = hba_ioctl_done_callback;
  1006. bi.done_context = NULL;
  1007. bi.offset = 0;
  1008. return handle_buffered_ioctl(&bi);
  1009. }
  1010. int esas2r_write_params(struct esas2r_adapter *a, struct esas2r_request *rq,
  1011. struct esas2r_sas_nvram *data)
  1012. {
  1013. int result = 0;
  1014. a->nvram_command_done = 0;
  1015. rq->comp_cb = complete_nvr_req;
  1016. if (esas2r_nvram_write(a, rq, data)) {
  1017. /* now wait around for it to complete. */
  1018. while (!a->nvram_command_done)
  1019. wait_event_interruptible(a->nvram_waiter,
  1020. a->nvram_command_done);
  1021. ;
  1022. /* done, check the status. */
  1023. if (rq->req_stat == RS_SUCCESS)
  1024. result = 1;
  1025. }
  1026. return result;
  1027. }
  1028. /* This function only cares about ATTO-specific ioctls (atto_express_ioctl) */
  1029. int esas2r_ioctl_handler(void *hostdata, unsigned int cmd, void __user *arg)
  1030. {
  1031. struct atto_express_ioctl *ioctl = NULL;
  1032. struct esas2r_adapter *a;
  1033. struct esas2r_request *rq;
  1034. u16 code;
  1035. int err;
  1036. esas2r_log(ESAS2R_LOG_DEBG, "ioctl (%p, %x, %p)", hostdata, cmd, arg);
  1037. if ((arg == NULL)
  1038. || (cmd < EXPRESS_IOCTL_MIN)
  1039. || (cmd > EXPRESS_IOCTL_MAX))
  1040. return -ENOTSUPP;
  1041. ioctl = memdup_user(arg, sizeof(struct atto_express_ioctl));
  1042. if (IS_ERR(ioctl)) {
  1043. esas2r_log(ESAS2R_LOG_WARN,
  1044. "ioctl_handler access_ok failed for cmd %u, address %p",
  1045. cmd, arg);
  1046. return PTR_ERR(ioctl);
  1047. }
  1048. /* verify the signature */
  1049. if (memcmp(ioctl->header.signature,
  1050. EXPRESS_IOCTL_SIGNATURE,
  1051. EXPRESS_IOCTL_SIGNATURE_SIZE) != 0) {
  1052. esas2r_log(ESAS2R_LOG_WARN, "invalid signature");
  1053. kfree(ioctl);
  1054. return -ENOTSUPP;
  1055. }
  1056. /* assume success */
  1057. ioctl->header.return_code = IOCTL_SUCCESS;
  1058. err = 0;
  1059. /*
  1060. * handle EXPRESS_IOCTL_GET_CHANNELS
  1061. * without paying attention to channel
  1062. */
  1063. if (cmd == EXPRESS_IOCTL_GET_CHANNELS) {
  1064. int i = 0, k = 0;
  1065. ioctl->data.chanlist.num_channels = 0;
  1066. while (i < MAX_ADAPTERS) {
  1067. if (esas2r_adapters[i]) {
  1068. ioctl->data.chanlist.num_channels++;
  1069. ioctl->data.chanlist.channel[k] = i;
  1070. k++;
  1071. }
  1072. i++;
  1073. }
  1074. goto ioctl_done;
  1075. }
  1076. /* get the channel */
  1077. if (ioctl->header.channel == 0xFF) {
  1078. a = (struct esas2r_adapter *)hostdata;
  1079. } else {
  1080. if (ioctl->header.channel >= MAX_ADAPTERS ||
  1081. esas2r_adapters[ioctl->header.channel] == NULL) {
  1082. ioctl->header.return_code = IOCTL_BAD_CHANNEL;
  1083. esas2r_log(ESAS2R_LOG_WARN, "bad channel value");
  1084. kfree(ioctl);
  1085. return -ENOTSUPP;
  1086. }
  1087. a = esas2r_adapters[ioctl->header.channel];
  1088. }
  1089. switch (cmd) {
  1090. case EXPRESS_IOCTL_RW_FIRMWARE:
  1091. if (ioctl->data.fwrw.img_type == FW_IMG_FM_API) {
  1092. err = esas2r_write_fw(a,
  1093. (char *)ioctl->data.fwrw.image,
  1094. 0,
  1095. sizeof(struct
  1096. atto_express_ioctl));
  1097. if (err >= 0) {
  1098. err = esas2r_read_fw(a,
  1099. (char *)ioctl->data.fwrw.
  1100. image,
  1101. 0,
  1102. sizeof(struct
  1103. atto_express_ioctl));
  1104. }
  1105. } else if (ioctl->data.fwrw.img_type == FW_IMG_FS_API) {
  1106. err = esas2r_write_fs(a,
  1107. (char *)ioctl->data.fwrw.image,
  1108. 0,
  1109. sizeof(struct
  1110. atto_express_ioctl));
  1111. if (err >= 0) {
  1112. err = esas2r_read_fs(a,
  1113. (char *)ioctl->data.fwrw.
  1114. image,
  1115. 0,
  1116. sizeof(struct
  1117. atto_express_ioctl));
  1118. }
  1119. } else {
  1120. ioctl->header.return_code = IOCTL_BAD_FLASH_IMGTYPE;
  1121. }
  1122. break;
  1123. case EXPRESS_IOCTL_READ_PARAMS:
  1124. memcpy(ioctl->data.prw.data_buffer, a->nvram,
  1125. sizeof(struct esas2r_sas_nvram));
  1126. ioctl->data.prw.code = 1;
  1127. break;
  1128. case EXPRESS_IOCTL_WRITE_PARAMS:
  1129. rq = esas2r_alloc_request(a);
  1130. if (rq == NULL) {
  1131. kfree(ioctl);
  1132. esas2r_log(ESAS2R_LOG_WARN,
  1133. "could not allocate an internal request");
  1134. return -ENOMEM;
  1135. }
  1136. code = esas2r_write_params(a, rq,
  1137. (struct esas2r_sas_nvram *)ioctl->data.prw.data_buffer);
  1138. ioctl->data.prw.code = code;
  1139. esas2r_free_request(a, rq);
  1140. break;
  1141. case EXPRESS_IOCTL_DEFAULT_PARAMS:
  1142. esas2r_nvram_get_defaults(a,
  1143. (struct esas2r_sas_nvram *)ioctl->data.prw.data_buffer);
  1144. ioctl->data.prw.code = 1;
  1145. break;
  1146. case EXPRESS_IOCTL_CHAN_INFO:
  1147. ioctl->data.chaninfo.major_rev = ESAS2R_MAJOR_REV;
  1148. ioctl->data.chaninfo.minor_rev = ESAS2R_MINOR_REV;
  1149. ioctl->data.chaninfo.IRQ = a->pcid->irq;
  1150. ioctl->data.chaninfo.device_id = a->pcid->device;
  1151. ioctl->data.chaninfo.vendor_id = a->pcid->vendor;
  1152. ioctl->data.chaninfo.ven_dev_id = a->pcid->subsystem_device;
  1153. ioctl->data.chaninfo.revision_id = a->pcid->revision;
  1154. ioctl->data.chaninfo.pci_bus = a->pcid->bus->number;
  1155. ioctl->data.chaninfo.pci_dev_func = a->pcid->devfn;
  1156. ioctl->data.chaninfo.core_rev = 0;
  1157. ioctl->data.chaninfo.host_no = a->host->host_no;
  1158. ioctl->data.chaninfo.hbaapi_rev = 0;
  1159. break;
  1160. case EXPRESS_IOCTL_SMP:
  1161. ioctl->header.return_code = handle_smp_ioctl(a,
  1162. &ioctl->data.
  1163. ioctl_smp);
  1164. break;
  1165. case EXPRESS_CSMI:
  1166. ioctl->header.return_code =
  1167. handle_csmi_ioctl(a, &ioctl->data.csmi);
  1168. break;
  1169. case EXPRESS_IOCTL_HBA:
  1170. ioctl->header.return_code = handle_hba_ioctl(a,
  1171. &ioctl->data.
  1172. ioctl_hba);
  1173. break;
  1174. case EXPRESS_IOCTL_VDA:
  1175. err = esas2r_write_vda(a,
  1176. (char *)&ioctl->data.ioctl_vda,
  1177. 0,
  1178. sizeof(struct atto_ioctl_vda) +
  1179. ioctl->data.ioctl_vda.data_length);
  1180. if (err >= 0) {
  1181. err = esas2r_read_vda(a,
  1182. (char *)&ioctl->data.ioctl_vda,
  1183. 0,
  1184. sizeof(struct atto_ioctl_vda) +
  1185. ioctl->data.ioctl_vda.data_length);
  1186. }
  1187. break;
  1188. case EXPRESS_IOCTL_GET_MOD_INFO:
  1189. ioctl->data.modinfo.adapter = a;
  1190. ioctl->data.modinfo.pci_dev = a->pcid;
  1191. ioctl->data.modinfo.scsi_host = a->host;
  1192. ioctl->data.modinfo.host_no = a->host->host_no;
  1193. break;
  1194. default:
  1195. esas2r_debug("esas2r_ioctl invalid cmd %p!", cmd);
  1196. ioctl->header.return_code = IOCTL_ERR_INVCMD;
  1197. }
  1198. ioctl_done:
  1199. if (err < 0) {
  1200. esas2r_log(ESAS2R_LOG_WARN, "err %d on ioctl cmd %u", err,
  1201. cmd);
  1202. switch (err) {
  1203. case -ENOMEM:
  1204. case -EBUSY:
  1205. ioctl->header.return_code = IOCTL_OUT_OF_RESOURCES;
  1206. break;
  1207. case -ENOSYS:
  1208. case -EINVAL:
  1209. ioctl->header.return_code = IOCTL_INVALID_PARAM;
  1210. break;
  1211. default:
  1212. ioctl->header.return_code = IOCTL_GENERAL_ERROR;
  1213. break;
  1214. }
  1215. }
  1216. /* Always copy the buffer back, if only to pick up the status */
  1217. err = copy_to_user(arg, ioctl, sizeof(struct atto_express_ioctl));
  1218. if (err != 0) {
  1219. esas2r_log(ESAS2R_LOG_WARN,
  1220. "ioctl_handler copy_to_user didn't copy everything (err %d, cmd %u)",
  1221. err, cmd);
  1222. kfree(ioctl);
  1223. return -EFAULT;
  1224. }
  1225. kfree(ioctl);
  1226. return 0;
  1227. }
  1228. int esas2r_ioctl(struct scsi_device *sd, unsigned int cmd, void __user *arg)
  1229. {
  1230. return esas2r_ioctl_handler(sd->host->hostdata, cmd, arg);
  1231. }
  1232. static void free_fw_buffers(struct esas2r_adapter *a)
  1233. {
  1234. if (a->firmware.data) {
  1235. dma_free_coherent(&a->pcid->dev,
  1236. (size_t)a->firmware.orig_len,
  1237. a->firmware.data,
  1238. (dma_addr_t)a->firmware.phys);
  1239. a->firmware.data = NULL;
  1240. }
  1241. }
  1242. static int allocate_fw_buffers(struct esas2r_adapter *a, u32 length)
  1243. {
  1244. free_fw_buffers(a);
  1245. a->firmware.orig_len = length;
  1246. a->firmware.data = dma_alloc_coherent(&a->pcid->dev,
  1247. (size_t)length,
  1248. (dma_addr_t *)&a->firmware.phys,
  1249. GFP_KERNEL);
  1250. if (!a->firmware.data) {
  1251. esas2r_debug("buffer alloc failed!");
  1252. return 0;
  1253. }
  1254. return 1;
  1255. }
  1256. /* Handle a call to read firmware. */
  1257. int esas2r_read_fw(struct esas2r_adapter *a, char *buf, long off, int count)
  1258. {
  1259. esas2r_trace_enter();
  1260. /* if the cached header is a status, simply copy it over and return. */
  1261. if (a->firmware.state == FW_STATUS_ST) {
  1262. int size = min_t(int, count, sizeof(a->firmware.header));
  1263. esas2r_trace_exit();
  1264. memcpy(buf, &a->firmware.header, size);
  1265. esas2r_debug("esas2r_read_fw: STATUS size %d", size);
  1266. return size;
  1267. }
  1268. /*
  1269. * if the cached header is a command, do it if at
  1270. * offset 0, otherwise copy the pieces.
  1271. */
  1272. if (a->firmware.state == FW_COMMAND_ST) {
  1273. u32 length = a->firmware.header.length;
  1274. esas2r_trace_exit();
  1275. esas2r_debug("esas2r_read_fw: COMMAND length %d off %d",
  1276. length,
  1277. off);
  1278. if (off == 0) {
  1279. if (a->firmware.header.action == FI_ACT_UP) {
  1280. if (!allocate_fw_buffers(a, length))
  1281. return -ENOMEM;
  1282. /* copy header over */
  1283. memcpy(a->firmware.data,
  1284. &a->firmware.header,
  1285. sizeof(a->firmware.header));
  1286. do_fm_api(a,
  1287. (struct esas2r_flash_img *)a->firmware.data);
  1288. } else if (a->firmware.header.action == FI_ACT_UPSZ) {
  1289. int size =
  1290. min((int)count,
  1291. (int)sizeof(a->firmware.header));
  1292. do_fm_api(a, &a->firmware.header);
  1293. memcpy(buf, &a->firmware.header, size);
  1294. esas2r_debug("FI_ACT_UPSZ size %d", size);
  1295. return size;
  1296. } else {
  1297. esas2r_debug("invalid action %d",
  1298. a->firmware.header.action);
  1299. return -ENOSYS;
  1300. }
  1301. }
  1302. if (count + off > length)
  1303. count = length - off;
  1304. if (count < 0)
  1305. return 0;
  1306. if (!a->firmware.data) {
  1307. esas2r_debug(
  1308. "read: nonzero offset but no buffer available!");
  1309. return -ENOMEM;
  1310. }
  1311. esas2r_debug("esas2r_read_fw: off %d count %d length %d ", off,
  1312. count,
  1313. length);
  1314. memcpy(buf, &a->firmware.data[off], count);
  1315. /* when done, release the buffer */
  1316. if (length <= off + count) {
  1317. esas2r_debug("esas2r_read_fw: freeing buffer!");
  1318. free_fw_buffers(a);
  1319. }
  1320. return count;
  1321. }
  1322. esas2r_trace_exit();
  1323. esas2r_debug("esas2r_read_fw: invalid firmware state %d",
  1324. a->firmware.state);
  1325. return -EINVAL;
  1326. }
  1327. /* Handle a call to write firmware. */
  1328. int esas2r_write_fw(struct esas2r_adapter *a, const char *buf, long off,
  1329. int count)
  1330. {
  1331. u32 length;
  1332. if (off == 0) {
  1333. struct esas2r_flash_img *header =
  1334. (struct esas2r_flash_img *)buf;
  1335. /* assume version 0 flash image */
  1336. int min_size = sizeof(struct esas2r_flash_img_v0);
  1337. a->firmware.state = FW_INVALID_ST;
  1338. /* validate the version field first */
  1339. if (count < 4
  1340. || header->fi_version > FI_VERSION_1) {
  1341. esas2r_debug(
  1342. "esas2r_write_fw: short header or invalid version");
  1343. return -EINVAL;
  1344. }
  1345. /* See if its a version 1 flash image */
  1346. if (header->fi_version == FI_VERSION_1)
  1347. min_size = sizeof(struct esas2r_flash_img);
  1348. /* If this is the start, the header must be full and valid. */
  1349. if (count < min_size) {
  1350. esas2r_debug("esas2r_write_fw: short header, aborting");
  1351. return -EINVAL;
  1352. }
  1353. /* Make sure the size is reasonable. */
  1354. length = header->length;
  1355. if (length > 1024 * 1024) {
  1356. esas2r_debug(
  1357. "esas2r_write_fw: hosed, length %d fi_version %d",
  1358. length, header->fi_version);
  1359. return -EINVAL;
  1360. }
  1361. /*
  1362. * If this is a write command, allocate memory because
  1363. * we have to cache everything. otherwise, just cache
  1364. * the header, because the read op will do the command.
  1365. */
  1366. if (header->action == FI_ACT_DOWN) {
  1367. if (!allocate_fw_buffers(a, length))
  1368. return -ENOMEM;
  1369. /*
  1370. * Store the command, so there is context on subsequent
  1371. * calls.
  1372. */
  1373. memcpy(&a->firmware.header,
  1374. buf,
  1375. sizeof(*header));
  1376. } else if (header->action == FI_ACT_UP
  1377. || header->action == FI_ACT_UPSZ) {
  1378. /* Save the command, result will be picked up on read */
  1379. memcpy(&a->firmware.header,
  1380. buf,
  1381. sizeof(*header));
  1382. a->firmware.state = FW_COMMAND_ST;
  1383. esas2r_debug(
  1384. "esas2r_write_fw: COMMAND, count %d, action %d ",
  1385. count, header->action);
  1386. /*
  1387. * Pretend we took the whole buffer,
  1388. * so we don't get bothered again.
  1389. */
  1390. return count;
  1391. } else {
  1392. esas2r_debug("esas2r_write_fw: invalid action %d ",
  1393. a->firmware.header.action);
  1394. return -ENOSYS;
  1395. }
  1396. } else {
  1397. length = a->firmware.header.length;
  1398. }
  1399. /*
  1400. * We only get here on a download command, regardless of offset.
  1401. * the chunks written by the system need to be cached, and when
  1402. * the final one arrives, issue the fmapi command.
  1403. */
  1404. if (off + count > length)
  1405. count = length - off;
  1406. if (count > 0) {
  1407. esas2r_debug("esas2r_write_fw: off %d count %d length %d", off,
  1408. count,
  1409. length);
  1410. /*
  1411. * On a full upload, the system tries sending the whole buffer.
  1412. * there's nothing to do with it, so just drop it here, before
  1413. * trying to copy over into unallocated memory!
  1414. */
  1415. if (a->firmware.header.action == FI_ACT_UP)
  1416. return count;
  1417. if (!a->firmware.data) {
  1418. esas2r_debug(
  1419. "write: nonzero offset but no buffer available!");
  1420. return -ENOMEM;
  1421. }
  1422. memcpy(&a->firmware.data[off], buf, count);
  1423. if (length == off + count) {
  1424. do_fm_api(a,
  1425. (struct esas2r_flash_img *)a->firmware.data);
  1426. /*
  1427. * Now copy the header result to be picked up by the
  1428. * next read
  1429. */
  1430. memcpy(&a->firmware.header,
  1431. a->firmware.data,
  1432. sizeof(a->firmware.header));
  1433. a->firmware.state = FW_STATUS_ST;
  1434. esas2r_debug("write completed");
  1435. /*
  1436. * Since the system has the data buffered, the only way
  1437. * this can leak is if a root user writes a program
  1438. * that writes a shorter buffer than it claims, and the
  1439. * copyin fails.
  1440. */
  1441. free_fw_buffers(a);
  1442. }
  1443. }
  1444. return count;
  1445. }
  1446. /* Callback for the completion of a VDA request. */
  1447. static void vda_complete_req(struct esas2r_adapter *a,
  1448. struct esas2r_request *rq)
  1449. {
  1450. a->vda_command_done = 1;
  1451. wake_up_interruptible(&a->vda_waiter);
  1452. }
  1453. /* Scatter/gather callback for VDA requests */
  1454. static u32 get_physaddr_vda(struct esas2r_sg_context *sgc, u64 *addr)
  1455. {
  1456. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  1457. int offset = (u8 *)sgc->cur_offset - (u8 *)a->vda_buffer;
  1458. (*addr) = a->ppvda_buffer + offset;
  1459. return VDA_MAX_BUFFER_SIZE - offset;
  1460. }
  1461. /* Handle a call to read a VDA command. */
  1462. int esas2r_read_vda(struct esas2r_adapter *a, char *buf, long off, int count)
  1463. {
  1464. if (!a->vda_buffer)
  1465. return -ENOMEM;
  1466. if (off == 0) {
  1467. struct esas2r_request *rq;
  1468. struct atto_ioctl_vda *vi =
  1469. (struct atto_ioctl_vda *)a->vda_buffer;
  1470. struct esas2r_sg_context sgc;
  1471. bool wait_for_completion;
  1472. /*
  1473. * Presumeably, someone has already written to the vda_buffer,
  1474. * and now they are reading the node the response, so now we
  1475. * will actually issue the request to the chip and reply.
  1476. */
  1477. /* allocate a request */
  1478. rq = esas2r_alloc_request(a);
  1479. if (rq == NULL) {
  1480. esas2r_debug("esas2r_read_vda: out of requests");
  1481. return -EBUSY;
  1482. }
  1483. rq->comp_cb = vda_complete_req;
  1484. sgc.first_req = rq;
  1485. sgc.adapter = a;
  1486. sgc.cur_offset = a->vda_buffer + VDA_BUFFER_HEADER_SZ;
  1487. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_vda;
  1488. a->vda_command_done = 0;
  1489. wait_for_completion =
  1490. esas2r_process_vda_ioctl(a, vi, rq, &sgc);
  1491. if (wait_for_completion) {
  1492. /* now wait around for it to complete. */
  1493. while (!a->vda_command_done)
  1494. wait_event_interruptible(a->vda_waiter,
  1495. a->vda_command_done);
  1496. }
  1497. esas2r_free_request(a, (struct esas2r_request *)rq);
  1498. }
  1499. if (off > VDA_MAX_BUFFER_SIZE)
  1500. return 0;
  1501. if (count + off > VDA_MAX_BUFFER_SIZE)
  1502. count = VDA_MAX_BUFFER_SIZE - off;
  1503. if (count < 0)
  1504. return 0;
  1505. memcpy(buf, a->vda_buffer + off, count);
  1506. return count;
  1507. }
  1508. /* Handle a call to write a VDA command. */
  1509. int esas2r_write_vda(struct esas2r_adapter *a, const char *buf, long off,
  1510. int count)
  1511. {
  1512. /*
  1513. * allocate memory for it, if not already done. once allocated,
  1514. * we will keep it around until the driver is unloaded.
  1515. */
  1516. if (!a->vda_buffer) {
  1517. dma_addr_t dma_addr;
  1518. a->vda_buffer = dma_alloc_coherent(&a->pcid->dev,
  1519. (size_t)
  1520. VDA_MAX_BUFFER_SIZE,
  1521. &dma_addr,
  1522. GFP_KERNEL);
  1523. a->ppvda_buffer = dma_addr;
  1524. }
  1525. if (!a->vda_buffer)
  1526. return -ENOMEM;
  1527. if (off > VDA_MAX_BUFFER_SIZE)
  1528. return 0;
  1529. if (count + off > VDA_MAX_BUFFER_SIZE)
  1530. count = VDA_MAX_BUFFER_SIZE - off;
  1531. if (count < 1)
  1532. return 0;
  1533. memcpy(a->vda_buffer + off, buf, count);
  1534. return count;
  1535. }
  1536. /* Callback for the completion of an FS_API request.*/
  1537. static void fs_api_complete_req(struct esas2r_adapter *a,
  1538. struct esas2r_request *rq)
  1539. {
  1540. a->fs_api_command_done = 1;
  1541. wake_up_interruptible(&a->fs_api_waiter);
  1542. }
  1543. /* Scatter/gather callback for VDA requests */
  1544. static u32 get_physaddr_fs_api(struct esas2r_sg_context *sgc, u64 *addr)
  1545. {
  1546. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  1547. struct esas2r_ioctl_fs *fs =
  1548. (struct esas2r_ioctl_fs *)a->fs_api_buffer;
  1549. u32 offset = (u8 *)sgc->cur_offset - (u8 *)fs;
  1550. (*addr) = a->ppfs_api_buffer + offset;
  1551. return a->fs_api_buffer_size - offset;
  1552. }
  1553. /* Handle a call to read firmware via FS_API. */
  1554. int esas2r_read_fs(struct esas2r_adapter *a, char *buf, long off, int count)
  1555. {
  1556. if (!a->fs_api_buffer)
  1557. return -ENOMEM;
  1558. if (off == 0) {
  1559. struct esas2r_request *rq;
  1560. struct esas2r_sg_context sgc;
  1561. struct esas2r_ioctl_fs *fs =
  1562. (struct esas2r_ioctl_fs *)a->fs_api_buffer;
  1563. /* If another flash request is already in progress, return. */
  1564. if (mutex_lock_interruptible(&a->fs_api_mutex)) {
  1565. busy:
  1566. fs->status = ATTO_STS_OUT_OF_RSRC;
  1567. return -EBUSY;
  1568. }
  1569. /*
  1570. * Presumeably, someone has already written to the
  1571. * fs_api_buffer, and now they are reading the node the
  1572. * response, so now we will actually issue the request to the
  1573. * chip and reply. Allocate a request
  1574. */
  1575. rq = esas2r_alloc_request(a);
  1576. if (rq == NULL) {
  1577. esas2r_debug("esas2r_read_fs: out of requests");
  1578. mutex_unlock(&a->fs_api_mutex);
  1579. goto busy;
  1580. }
  1581. rq->comp_cb = fs_api_complete_req;
  1582. /* Set up the SGCONTEXT for to build the s/g table */
  1583. sgc.cur_offset = fs->data;
  1584. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_fs_api;
  1585. a->fs_api_command_done = 0;
  1586. if (!esas2r_process_fs_ioctl(a, fs, rq, &sgc)) {
  1587. if (fs->status == ATTO_STS_OUT_OF_RSRC)
  1588. count = -EBUSY;
  1589. goto dont_wait;
  1590. }
  1591. /* Now wait around for it to complete. */
  1592. while (!a->fs_api_command_done)
  1593. wait_event_interruptible(a->fs_api_waiter,
  1594. a->fs_api_command_done);
  1595. ;
  1596. dont_wait:
  1597. /* Free the request and keep going */
  1598. mutex_unlock(&a->fs_api_mutex);
  1599. esas2r_free_request(a, (struct esas2r_request *)rq);
  1600. /* Pick up possible error code from above */
  1601. if (count < 0)
  1602. return count;
  1603. }
  1604. if (off > a->fs_api_buffer_size)
  1605. return 0;
  1606. if (count + off > a->fs_api_buffer_size)
  1607. count = a->fs_api_buffer_size - off;
  1608. if (count < 0)
  1609. return 0;
  1610. memcpy(buf, a->fs_api_buffer + off, count);
  1611. return count;
  1612. }
  1613. /* Handle a call to write firmware via FS_API. */
  1614. int esas2r_write_fs(struct esas2r_adapter *a, const char *buf, long off,
  1615. int count)
  1616. {
  1617. if (off == 0) {
  1618. struct esas2r_ioctl_fs *fs = (struct esas2r_ioctl_fs *)buf;
  1619. u32 length = fs->command.length + offsetof(
  1620. struct esas2r_ioctl_fs,
  1621. data);
  1622. /*
  1623. * Special case, for BEGIN commands, the length field
  1624. * is lying to us, so just get enough for the header.
  1625. */
  1626. if (fs->command.command == ESAS2R_FS_CMD_BEGINW)
  1627. length = offsetof(struct esas2r_ioctl_fs, data);
  1628. /*
  1629. * Beginning a command. We assume we'll get at least
  1630. * enough in the first write so we can look at the
  1631. * header and see how much we need to alloc.
  1632. */
  1633. if (count < offsetof(struct esas2r_ioctl_fs, data))
  1634. return -EINVAL;
  1635. /* Allocate a buffer or use the existing buffer. */
  1636. if (a->fs_api_buffer) {
  1637. if (a->fs_api_buffer_size < length) {
  1638. /* Free too-small buffer and get a new one */
  1639. dma_free_coherent(&a->pcid->dev,
  1640. (size_t)a->fs_api_buffer_size,
  1641. a->fs_api_buffer,
  1642. (dma_addr_t)a->ppfs_api_buffer);
  1643. goto re_allocate_buffer;
  1644. }
  1645. } else {
  1646. re_allocate_buffer:
  1647. a->fs_api_buffer_size = length;
  1648. a->fs_api_buffer = dma_alloc_coherent(&a->pcid->dev,
  1649. (size_t)a->fs_api_buffer_size,
  1650. (dma_addr_t *)&a->ppfs_api_buffer,
  1651. GFP_KERNEL);
  1652. }
  1653. }
  1654. if (!a->fs_api_buffer)
  1655. return -ENOMEM;
  1656. if (off > a->fs_api_buffer_size)
  1657. return 0;
  1658. if (count + off > a->fs_api_buffer_size)
  1659. count = a->fs_api_buffer_size - off;
  1660. if (count < 1)
  1661. return 0;
  1662. memcpy(a->fs_api_buffer + off, buf, count);
  1663. return count;
  1664. }