esas2r_ioctl.c 51 KB

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