esas2r_ioctl.c 51 KB

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