ibmvscsi.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * TODO: This is currently pretty tied to the IBM pSeries hypervisor
  59. * interfaces. It would be really nice to abstract this above an RDMA
  60. * layer.
  61. */
  62. #include <linux/module.h>
  63. #include <linux/moduleparam.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/delay.h>
  66. #include <linux/slab.h>
  67. #include <linux/of.h>
  68. #include <linux/pm.h>
  69. #include <linux/kthread.h>
  70. #include <asm/firmware.h>
  71. #include <asm/vio.h>
  72. #include <scsi/scsi.h>
  73. #include <scsi/scsi_cmnd.h>
  74. #include <scsi/scsi_host.h>
  75. #include <scsi/scsi_device.h>
  76. #include <scsi/scsi_transport_srp.h>
  77. #include "ibmvscsi.h"
  78. /* The values below are somewhat arbitrary default values, but
  79. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  80. * Note that there are 3 bits of channel value, 6 bits of id, and
  81. * 5 bits of LUN.
  82. */
  83. static int max_id = 64;
  84. static int max_channel = 3;
  85. static int init_timeout = 300;
  86. static int login_timeout = 60;
  87. static int info_timeout = 30;
  88. static int abort_timeout = 60;
  89. static int reset_timeout = 60;
  90. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  91. static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
  92. static int fast_fail = 1;
  93. static int client_reserve = 1;
  94. static char partition_name[97] = "UNKNOWN";
  95. static unsigned int partition_number = -1;
  96. static struct scsi_transport_template *ibmvscsi_transport_template;
  97. #define IBMVSCSI_VERSION "1.5.9"
  98. MODULE_DESCRIPTION("IBM Virtual SCSI");
  99. MODULE_AUTHOR("Dave Boutcher");
  100. MODULE_LICENSE("GPL");
  101. MODULE_VERSION(IBMVSCSI_VERSION);
  102. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  103. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  104. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  105. MODULE_PARM_DESC(max_channel, "Largest channel value");
  106. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  107. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  108. module_param_named(max_requests, max_requests, int, S_IRUGO);
  109. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  110. module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
  111. MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
  112. module_param_named(client_reserve, client_reserve, int, S_IRUGO );
  113. MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
  114. static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  115. struct ibmvscsi_host_data *hostdata);
  116. /* ------------------------------------------------------------
  117. * Routines for managing the command/response queue
  118. */
  119. /**
  120. * ibmvscsi_handle_event: - Interrupt handler for crq events
  121. * @irq: number of irq to handle, not used
  122. * @dev_instance: ibmvscsi_host_data of host that received interrupt
  123. *
  124. * Disables interrupts and schedules srp_task
  125. * Always returns IRQ_HANDLED
  126. */
  127. static irqreturn_t ibmvscsi_handle_event(int irq, void *dev_instance)
  128. {
  129. struct ibmvscsi_host_data *hostdata =
  130. (struct ibmvscsi_host_data *)dev_instance;
  131. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  132. tasklet_schedule(&hostdata->srp_task);
  133. return IRQ_HANDLED;
  134. }
  135. /**
  136. * release_crq_queue: - Deallocates data and unregisters CRQ
  137. * @queue: crq_queue to initialize and register
  138. * @host_data: ibmvscsi_host_data of host
  139. *
  140. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  141. * the crq with the hypervisor.
  142. */
  143. static void ibmvscsi_release_crq_queue(struct crq_queue *queue,
  144. struct ibmvscsi_host_data *hostdata,
  145. int max_requests)
  146. {
  147. long rc = 0;
  148. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  149. free_irq(vdev->irq, (void *)hostdata);
  150. tasklet_kill(&hostdata->srp_task);
  151. do {
  152. if (rc)
  153. msleep(100);
  154. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  155. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  156. dma_unmap_single(hostdata->dev,
  157. queue->msg_token,
  158. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  159. free_page((unsigned long)queue->msgs);
  160. }
  161. /**
  162. * crq_queue_next_crq: - Returns the next entry in message queue
  163. * @queue: crq_queue to use
  164. *
  165. * Returns pointer to next entry in queue, or NULL if there are no new
  166. * entried in the CRQ.
  167. */
  168. static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
  169. {
  170. struct viosrp_crq *crq;
  171. unsigned long flags;
  172. spin_lock_irqsave(&queue->lock, flags);
  173. crq = &queue->msgs[queue->cur];
  174. if (crq->valid & 0x80) {
  175. if (++queue->cur == queue->size)
  176. queue->cur = 0;
  177. } else
  178. crq = NULL;
  179. spin_unlock_irqrestore(&queue->lock, flags);
  180. return crq;
  181. }
  182. /**
  183. * ibmvscsi_send_crq: - Send a CRQ
  184. * @hostdata: the adapter
  185. * @word1: the first 64 bits of the data
  186. * @word2: the second 64 bits of the data
  187. */
  188. static int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata,
  189. u64 word1, u64 word2)
  190. {
  191. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  192. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  193. }
  194. /**
  195. * ibmvscsi_task: - Process srps asynchronously
  196. * @data: ibmvscsi_host_data of host
  197. */
  198. static void ibmvscsi_task(void *data)
  199. {
  200. struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
  201. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  202. struct viosrp_crq *crq;
  203. int done = 0;
  204. while (!done) {
  205. /* Pull all the valid messages off the CRQ */
  206. while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  207. ibmvscsi_handle_crq(crq, hostdata);
  208. crq->valid = 0x00;
  209. }
  210. vio_enable_interrupts(vdev);
  211. crq = crq_queue_next_crq(&hostdata->queue);
  212. if (crq != NULL) {
  213. vio_disable_interrupts(vdev);
  214. ibmvscsi_handle_crq(crq, hostdata);
  215. crq->valid = 0x00;
  216. } else {
  217. done = 1;
  218. }
  219. }
  220. }
  221. static void gather_partition_info(void)
  222. {
  223. struct device_node *rootdn;
  224. const char *ppartition_name;
  225. const __be32 *p_number_ptr;
  226. /* Retrieve information about this partition */
  227. rootdn = of_find_node_by_path("/");
  228. if (!rootdn) {
  229. return;
  230. }
  231. ppartition_name = of_get_property(rootdn, "ibm,partition-name", NULL);
  232. if (ppartition_name)
  233. strncpy(partition_name, ppartition_name,
  234. sizeof(partition_name));
  235. p_number_ptr = of_get_property(rootdn, "ibm,partition-no", NULL);
  236. if (p_number_ptr)
  237. partition_number = of_read_number(p_number_ptr, 1);
  238. of_node_put(rootdn);
  239. }
  240. static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
  241. {
  242. memset(&hostdata->madapter_info, 0x00,
  243. sizeof(hostdata->madapter_info));
  244. dev_info(hostdata->dev, "SRP_VERSION: %s\n", SRP_VERSION);
  245. strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
  246. strncpy(hostdata->madapter_info.partition_name, partition_name,
  247. sizeof(hostdata->madapter_info.partition_name));
  248. hostdata->madapter_info.partition_number =
  249. cpu_to_be32(partition_number);
  250. hostdata->madapter_info.mad_version = cpu_to_be32(1);
  251. hostdata->madapter_info.os_type = cpu_to_be32(2);
  252. }
  253. /**
  254. * reset_crq_queue: - resets a crq after a failure
  255. * @queue: crq_queue to initialize and register
  256. * @hostdata: ibmvscsi_host_data of host
  257. *
  258. */
  259. static int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
  260. struct ibmvscsi_host_data *hostdata)
  261. {
  262. int rc = 0;
  263. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  264. /* Close the CRQ */
  265. do {
  266. if (rc)
  267. msleep(100);
  268. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  269. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  270. /* Clean out the queue */
  271. memset(queue->msgs, 0x00, PAGE_SIZE);
  272. queue->cur = 0;
  273. set_adapter_info(hostdata);
  274. /* And re-open it again */
  275. rc = plpar_hcall_norets(H_REG_CRQ,
  276. vdev->unit_address,
  277. queue->msg_token, PAGE_SIZE);
  278. if (rc == 2) {
  279. /* Adapter is good, but other end is not ready */
  280. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  281. } else if (rc != 0) {
  282. dev_warn(hostdata->dev, "couldn't register crq--rc 0x%x\n", rc);
  283. }
  284. return rc;
  285. }
  286. /**
  287. * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
  288. * @queue: crq_queue to initialize and register
  289. * @hostdata: ibmvscsi_host_data of host
  290. *
  291. * Allocates a page for messages, maps it for dma, and registers
  292. * the crq with the hypervisor.
  293. * Returns zero on success.
  294. */
  295. static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
  296. struct ibmvscsi_host_data *hostdata,
  297. int max_requests)
  298. {
  299. int rc;
  300. int retrc;
  301. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  302. queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  303. if (!queue->msgs)
  304. goto malloc_failed;
  305. queue->size = PAGE_SIZE / sizeof(*queue->msgs);
  306. queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
  307. queue->size * sizeof(*queue->msgs),
  308. DMA_BIDIRECTIONAL);
  309. if (dma_mapping_error(hostdata->dev, queue->msg_token))
  310. goto map_failed;
  311. gather_partition_info();
  312. set_adapter_info(hostdata);
  313. retrc = rc = plpar_hcall_norets(H_REG_CRQ,
  314. vdev->unit_address,
  315. queue->msg_token, PAGE_SIZE);
  316. if (rc == H_RESOURCE)
  317. /* maybe kexecing and resource is busy. try a reset */
  318. rc = ibmvscsi_reset_crq_queue(queue,
  319. hostdata);
  320. if (rc == 2) {
  321. /* Adapter is good, but other end is not ready */
  322. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  323. retrc = 0;
  324. } else if (rc != 0) {
  325. dev_warn(hostdata->dev, "Error %d opening adapter\n", rc);
  326. goto reg_crq_failed;
  327. }
  328. queue->cur = 0;
  329. spin_lock_init(&queue->lock);
  330. tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
  331. (unsigned long)hostdata);
  332. if (request_irq(vdev->irq,
  333. ibmvscsi_handle_event,
  334. 0, "ibmvscsi", (void *)hostdata) != 0) {
  335. dev_err(hostdata->dev, "couldn't register irq 0x%x\n",
  336. vdev->irq);
  337. goto req_irq_failed;
  338. }
  339. rc = vio_enable_interrupts(vdev);
  340. if (rc != 0) {
  341. dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc);
  342. goto req_irq_failed;
  343. }
  344. return retrc;
  345. req_irq_failed:
  346. tasklet_kill(&hostdata->srp_task);
  347. rc = 0;
  348. do {
  349. if (rc)
  350. msleep(100);
  351. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  352. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  353. reg_crq_failed:
  354. dma_unmap_single(hostdata->dev,
  355. queue->msg_token,
  356. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  357. map_failed:
  358. free_page((unsigned long)queue->msgs);
  359. malloc_failed:
  360. return -1;
  361. }
  362. /**
  363. * reenable_crq_queue: - reenables a crq after
  364. * @queue: crq_queue to initialize and register
  365. * @hostdata: ibmvscsi_host_data of host
  366. *
  367. */
  368. static int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
  369. struct ibmvscsi_host_data *hostdata)
  370. {
  371. int rc = 0;
  372. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  373. /* Re-enable the CRQ */
  374. do {
  375. if (rc)
  376. msleep(100);
  377. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  378. } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  379. if (rc)
  380. dev_err(hostdata->dev, "Error %d enabling adapter\n", rc);
  381. return rc;
  382. }
  383. /* ------------------------------------------------------------
  384. * Routines for the event pool and event structs
  385. */
  386. /**
  387. * initialize_event_pool: - Allocates and initializes the event pool for a host
  388. * @pool: event_pool to be initialized
  389. * @size: Number of events in pool
  390. * @hostdata: ibmvscsi_host_data who owns the event pool
  391. *
  392. * Returns zero on success.
  393. */
  394. static int initialize_event_pool(struct event_pool *pool,
  395. int size, struct ibmvscsi_host_data *hostdata)
  396. {
  397. int i;
  398. pool->size = size;
  399. pool->next = 0;
  400. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  401. if (!pool->events)
  402. return -ENOMEM;
  403. pool->iu_storage =
  404. dma_alloc_coherent(hostdata->dev,
  405. pool->size * sizeof(*pool->iu_storage),
  406. &pool->iu_token, 0);
  407. if (!pool->iu_storage) {
  408. kfree(pool->events);
  409. return -ENOMEM;
  410. }
  411. for (i = 0; i < pool->size; ++i) {
  412. struct srp_event_struct *evt = &pool->events[i];
  413. memset(&evt->crq, 0x00, sizeof(evt->crq));
  414. atomic_set(&evt->free, 1);
  415. evt->crq.valid = 0x80;
  416. evt->crq.IU_length = cpu_to_be16(sizeof(*evt->xfer_iu));
  417. evt->crq.IU_data_ptr = cpu_to_be64(pool->iu_token +
  418. sizeof(*evt->xfer_iu) * i);
  419. evt->xfer_iu = pool->iu_storage + i;
  420. evt->hostdata = hostdata;
  421. evt->ext_list = NULL;
  422. evt->ext_list_token = 0;
  423. }
  424. return 0;
  425. }
  426. /**
  427. * release_event_pool: - Frees memory of an event pool of a host
  428. * @pool: event_pool to be released
  429. * @hostdata: ibmvscsi_host_data who owns the even pool
  430. *
  431. * Returns zero on success.
  432. */
  433. static void release_event_pool(struct event_pool *pool,
  434. struct ibmvscsi_host_data *hostdata)
  435. {
  436. int i, in_use = 0;
  437. for (i = 0; i < pool->size; ++i) {
  438. if (atomic_read(&pool->events[i].free) != 1)
  439. ++in_use;
  440. if (pool->events[i].ext_list) {
  441. dma_free_coherent(hostdata->dev,
  442. SG_ALL * sizeof(struct srp_direct_buf),
  443. pool->events[i].ext_list,
  444. pool->events[i].ext_list_token);
  445. }
  446. }
  447. if (in_use)
  448. dev_warn(hostdata->dev, "releasing event pool with %d "
  449. "events still in use?\n", in_use);
  450. kfree(pool->events);
  451. dma_free_coherent(hostdata->dev,
  452. pool->size * sizeof(*pool->iu_storage),
  453. pool->iu_storage, pool->iu_token);
  454. }
  455. /**
  456. * valid_event_struct: - Determines if event is valid.
  457. * @pool: event_pool that contains the event
  458. * @evt: srp_event_struct to be checked for validity
  459. *
  460. * Returns zero if event is invalid, one otherwise.
  461. */
  462. static int valid_event_struct(struct event_pool *pool,
  463. struct srp_event_struct *evt)
  464. {
  465. int index = evt - pool->events;
  466. if (index < 0 || index >= pool->size) /* outside of bounds */
  467. return 0;
  468. if (evt != pool->events + index) /* unaligned */
  469. return 0;
  470. return 1;
  471. }
  472. /**
  473. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  474. * @pool: event_pool that contains the event
  475. * @evt: srp_event_struct to be modified
  476. *
  477. */
  478. static void free_event_struct(struct event_pool *pool,
  479. struct srp_event_struct *evt)
  480. {
  481. if (!valid_event_struct(pool, evt)) {
  482. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  483. "(not in pool %p)\n", evt, pool->events);
  484. return;
  485. }
  486. if (atomic_inc_return(&evt->free) != 1) {
  487. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  488. "which is not in use!\n", evt);
  489. return;
  490. }
  491. }
  492. /**
  493. * get_evt_struct: - Gets the next free event in pool
  494. * @pool: event_pool that contains the events to be searched
  495. *
  496. * Returns the next event in "free" state, and NULL if none are free.
  497. * Note that no synchronization is done here, we assume the host_lock
  498. * will syncrhonze things.
  499. */
  500. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  501. {
  502. int i;
  503. int poolsize = pool->size;
  504. int offset = pool->next;
  505. for (i = 0; i < poolsize; i++) {
  506. offset = (offset + 1) % poolsize;
  507. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  508. pool->next = offset;
  509. return &pool->events[offset];
  510. }
  511. }
  512. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  513. return NULL;
  514. }
  515. /**
  516. * init_event_struct: Initialize fields in an event struct that are always
  517. * required.
  518. * @evt: The event
  519. * @done: Routine to call when the event is responded to
  520. * @format: SRP or MAD format
  521. * @timeout: timeout value set in the CRQ
  522. */
  523. static void init_event_struct(struct srp_event_struct *evt_struct,
  524. void (*done) (struct srp_event_struct *),
  525. u8 format,
  526. int timeout)
  527. {
  528. evt_struct->cmnd = NULL;
  529. evt_struct->cmnd_done = NULL;
  530. evt_struct->sync_srp = NULL;
  531. evt_struct->crq.format = format;
  532. evt_struct->crq.timeout = cpu_to_be16(timeout);
  533. evt_struct->done = done;
  534. }
  535. /* ------------------------------------------------------------
  536. * Routines for receiving SCSI responses from the hosting partition
  537. */
  538. /**
  539. * set_srp_direction: Set the fields in the srp related to data
  540. * direction and number of buffers based on the direction in
  541. * the scsi_cmnd and the number of buffers
  542. */
  543. static void set_srp_direction(struct scsi_cmnd *cmd,
  544. struct srp_cmd *srp_cmd,
  545. int numbuf)
  546. {
  547. u8 fmt;
  548. if (numbuf == 0)
  549. return;
  550. if (numbuf == 1)
  551. fmt = SRP_DATA_DESC_DIRECT;
  552. else {
  553. fmt = SRP_DATA_DESC_INDIRECT;
  554. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  555. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  556. srp_cmd->data_out_desc_cnt = numbuf;
  557. else
  558. srp_cmd->data_in_desc_cnt = numbuf;
  559. }
  560. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  561. srp_cmd->buf_fmt = fmt << 4;
  562. else
  563. srp_cmd->buf_fmt = fmt;
  564. }
  565. /**
  566. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  567. * @cmd: srp_cmd whose additional_data member will be unmapped
  568. * @dev: device for which the memory is mapped
  569. *
  570. */
  571. static void unmap_cmd_data(struct srp_cmd *cmd,
  572. struct srp_event_struct *evt_struct,
  573. struct device *dev)
  574. {
  575. u8 out_fmt, in_fmt;
  576. out_fmt = cmd->buf_fmt >> 4;
  577. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  578. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  579. return;
  580. if (evt_struct->cmnd)
  581. scsi_dma_unmap(evt_struct->cmnd);
  582. }
  583. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  584. struct srp_direct_buf *md)
  585. {
  586. int i;
  587. struct scatterlist *sg;
  588. u64 total_length = 0;
  589. scsi_for_each_sg(cmd, sg, nseg, i) {
  590. struct srp_direct_buf *descr = md + i;
  591. descr->va = cpu_to_be64(sg_dma_address(sg));
  592. descr->len = cpu_to_be32(sg_dma_len(sg));
  593. descr->key = 0;
  594. total_length += sg_dma_len(sg);
  595. }
  596. return total_length;
  597. }
  598. /**
  599. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  600. * @cmd: Scsi_Cmnd with the scatterlist
  601. * @srp_cmd: srp_cmd that contains the memory descriptor
  602. * @dev: device for which to map dma memory
  603. *
  604. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  605. * Returns 1 on success.
  606. */
  607. static int map_sg_data(struct scsi_cmnd *cmd,
  608. struct srp_event_struct *evt_struct,
  609. struct srp_cmd *srp_cmd, struct device *dev)
  610. {
  611. int sg_mapped;
  612. u64 total_length = 0;
  613. struct srp_direct_buf *data =
  614. (struct srp_direct_buf *) srp_cmd->add_data;
  615. struct srp_indirect_buf *indirect =
  616. (struct srp_indirect_buf *) data;
  617. sg_mapped = scsi_dma_map(cmd);
  618. if (!sg_mapped)
  619. return 1;
  620. else if (sg_mapped < 0)
  621. return 0;
  622. set_srp_direction(cmd, srp_cmd, sg_mapped);
  623. /* special case; we can use a single direct descriptor */
  624. if (sg_mapped == 1) {
  625. map_sg_list(cmd, sg_mapped, data);
  626. return 1;
  627. }
  628. indirect->table_desc.va = 0;
  629. indirect->table_desc.len = cpu_to_be32(sg_mapped *
  630. sizeof(struct srp_direct_buf));
  631. indirect->table_desc.key = 0;
  632. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  633. total_length = map_sg_list(cmd, sg_mapped,
  634. &indirect->desc_list[0]);
  635. indirect->len = cpu_to_be32(total_length);
  636. return 1;
  637. }
  638. /* get indirect table */
  639. if (!evt_struct->ext_list) {
  640. evt_struct->ext_list = (struct srp_direct_buf *)
  641. dma_alloc_coherent(dev,
  642. SG_ALL * sizeof(struct srp_direct_buf),
  643. &evt_struct->ext_list_token, 0);
  644. if (!evt_struct->ext_list) {
  645. if (!firmware_has_feature(FW_FEATURE_CMO))
  646. sdev_printk(KERN_ERR, cmd->device,
  647. "Can't allocate memory "
  648. "for indirect table\n");
  649. scsi_dma_unmap(cmd);
  650. return 0;
  651. }
  652. }
  653. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  654. indirect->len = cpu_to_be32(total_length);
  655. indirect->table_desc.va = cpu_to_be64(evt_struct->ext_list_token);
  656. indirect->table_desc.len = cpu_to_be32(sg_mapped *
  657. sizeof(indirect->desc_list[0]));
  658. memcpy(indirect->desc_list, evt_struct->ext_list,
  659. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  660. return 1;
  661. }
  662. /**
  663. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  664. * @cmd: struct scsi_cmnd with the memory to be mapped
  665. * @srp_cmd: srp_cmd that contains the memory descriptor
  666. * @dev: dma device for which to map dma memory
  667. *
  668. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  669. * Returns 1 on success.
  670. */
  671. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  672. struct srp_event_struct *evt_struct,
  673. struct srp_cmd *srp_cmd, struct device *dev)
  674. {
  675. switch (cmd->sc_data_direction) {
  676. case DMA_FROM_DEVICE:
  677. case DMA_TO_DEVICE:
  678. break;
  679. case DMA_NONE:
  680. return 1;
  681. case DMA_BIDIRECTIONAL:
  682. sdev_printk(KERN_ERR, cmd->device,
  683. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  684. return 0;
  685. default:
  686. sdev_printk(KERN_ERR, cmd->device,
  687. "Unknown data direction 0x%02x; can't map!\n",
  688. cmd->sc_data_direction);
  689. return 0;
  690. }
  691. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  692. }
  693. /**
  694. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  695. * @hostdata: the adapter
  696. */
  697. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  698. {
  699. struct srp_event_struct *evt;
  700. unsigned long flags;
  701. spin_lock_irqsave(hostdata->host->host_lock, flags);
  702. while (!list_empty(&hostdata->sent)) {
  703. evt = list_first_entry(&hostdata->sent, struct srp_event_struct, list);
  704. list_del(&evt->list);
  705. del_timer(&evt->timer);
  706. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  707. if (evt->cmnd) {
  708. evt->cmnd->result = (error_code << 16);
  709. unmap_cmd_data(&evt->iu.srp.cmd, evt,
  710. evt->hostdata->dev);
  711. if (evt->cmnd_done)
  712. evt->cmnd_done(evt->cmnd);
  713. } else if (evt->done)
  714. evt->done(evt);
  715. free_event_struct(&evt->hostdata->pool, evt);
  716. spin_lock_irqsave(hostdata->host->host_lock, flags);
  717. }
  718. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  719. }
  720. /**
  721. * ibmvscsi_reset_host - Reset the connection to the server
  722. * @hostdata: struct ibmvscsi_host_data to reset
  723. */
  724. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  725. {
  726. scsi_block_requests(hostdata->host);
  727. atomic_set(&hostdata->request_limit, 0);
  728. purge_requests(hostdata, DID_ERROR);
  729. hostdata->reset_crq = 1;
  730. wake_up(&hostdata->work_wait_q);
  731. }
  732. /**
  733. * ibmvscsi_timeout - Internal command timeout handler
  734. * @evt_struct: struct srp_event_struct that timed out
  735. *
  736. * Called when an internally generated command times out
  737. */
  738. static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
  739. {
  740. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  741. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  742. evt_struct->iu.srp.cmd.opcode);
  743. ibmvscsi_reset_host(hostdata);
  744. }
  745. /* ------------------------------------------------------------
  746. * Routines for sending and receiving SRPs
  747. */
  748. /**
  749. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  750. * @evt_struct: evt_struct to be sent
  751. * @hostdata: ibmvscsi_host_data of host
  752. * @timeout: timeout in seconds - 0 means do not time command
  753. *
  754. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  755. * Note that this routine assumes that host_lock is held for synchronization
  756. */
  757. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  758. struct ibmvscsi_host_data *hostdata,
  759. unsigned long timeout)
  760. {
  761. __be64 *crq_as_u64 = (__be64 *)&evt_struct->crq;
  762. int request_status = 0;
  763. int rc;
  764. int srp_req = 0;
  765. /* If we have exhausted our request limit, just fail this request,
  766. * unless it is for a reset or abort.
  767. * Note that there are rare cases involving driver generated requests
  768. * (such as task management requests) that the mid layer may think we
  769. * can handle more requests (can_queue) when we actually can't
  770. */
  771. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  772. srp_req = 1;
  773. request_status =
  774. atomic_dec_if_positive(&hostdata->request_limit);
  775. /* If request limit was -1 when we started, it is now even
  776. * less than that
  777. */
  778. if (request_status < -1)
  779. goto send_error;
  780. /* Otherwise, we may have run out of requests. */
  781. /* If request limit was 0 when we started the adapter is in the
  782. * process of performing a login with the server adapter, or
  783. * we may have run out of requests.
  784. */
  785. else if (request_status == -1 &&
  786. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  787. goto send_busy;
  788. /* Abort and reset calls should make it through.
  789. * Nothing except abort and reset should use the last two
  790. * slots unless we had two or less to begin with.
  791. */
  792. else if (request_status < 2 &&
  793. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  794. /* In the case that we have less than two requests
  795. * available, check the server limit as a combination
  796. * of the request limit and the number of requests
  797. * in-flight (the size of the send list). If the
  798. * server limit is greater than 2, return busy so
  799. * that the last two are reserved for reset and abort.
  800. */
  801. int server_limit = request_status;
  802. struct srp_event_struct *tmp_evt;
  803. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  804. server_limit++;
  805. }
  806. if (server_limit > 2)
  807. goto send_busy;
  808. }
  809. }
  810. /* Copy the IU into the transfer area */
  811. *evt_struct->xfer_iu = evt_struct->iu;
  812. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  813. /* Add this to the sent list. We need to do this
  814. * before we actually send
  815. * in case it comes back REALLY fast
  816. */
  817. list_add_tail(&evt_struct->list, &hostdata->sent);
  818. init_timer(&evt_struct->timer);
  819. if (timeout) {
  820. evt_struct->timer.data = (unsigned long) evt_struct;
  821. evt_struct->timer.expires = jiffies + (timeout * HZ);
  822. evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
  823. add_timer(&evt_struct->timer);
  824. }
  825. rc = ibmvscsi_send_crq(hostdata, be64_to_cpu(crq_as_u64[0]),
  826. be64_to_cpu(crq_as_u64[1]));
  827. if (rc != 0) {
  828. list_del(&evt_struct->list);
  829. del_timer(&evt_struct->timer);
  830. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  831. * Firmware will send a CRQ with a transport event (0xFF) to
  832. * tell this client what has happened to the transport. This
  833. * will be handled in ibmvscsi_handle_crq()
  834. */
  835. if (rc == H_CLOSED) {
  836. dev_warn(hostdata->dev, "send warning. "
  837. "Receive queue closed, will retry.\n");
  838. goto send_busy;
  839. }
  840. dev_err(hostdata->dev, "send error %d\n", rc);
  841. if (srp_req)
  842. atomic_inc(&hostdata->request_limit);
  843. goto send_error;
  844. }
  845. return 0;
  846. send_busy:
  847. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  848. free_event_struct(&hostdata->pool, evt_struct);
  849. if (srp_req && request_status != -1)
  850. atomic_inc(&hostdata->request_limit);
  851. return SCSI_MLQUEUE_HOST_BUSY;
  852. send_error:
  853. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  854. if (evt_struct->cmnd != NULL) {
  855. evt_struct->cmnd->result = DID_ERROR << 16;
  856. evt_struct->cmnd_done(evt_struct->cmnd);
  857. } else if (evt_struct->done)
  858. evt_struct->done(evt_struct);
  859. free_event_struct(&hostdata->pool, evt_struct);
  860. return 0;
  861. }
  862. /**
  863. * handle_cmd_rsp: - Handle responses from commands
  864. * @evt_struct: srp_event_struct to be handled
  865. *
  866. * Used as a callback by when sending scsi cmds.
  867. * Gets called by ibmvscsi_handle_crq()
  868. */
  869. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  870. {
  871. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  872. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  873. if (unlikely(rsp->opcode != SRP_RSP)) {
  874. if (printk_ratelimit())
  875. dev_warn(evt_struct->hostdata->dev,
  876. "bad SRP RSP type %d\n", rsp->opcode);
  877. }
  878. if (cmnd) {
  879. cmnd->result |= rsp->status;
  880. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  881. memcpy(cmnd->sense_buffer,
  882. rsp->data,
  883. be32_to_cpu(rsp->sense_data_len));
  884. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  885. evt_struct,
  886. evt_struct->hostdata->dev);
  887. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  888. scsi_set_resid(cmnd,
  889. be32_to_cpu(rsp->data_out_res_cnt));
  890. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  891. scsi_set_resid(cmnd, be32_to_cpu(rsp->data_in_res_cnt));
  892. }
  893. if (evt_struct->cmnd_done)
  894. evt_struct->cmnd_done(cmnd);
  895. }
  896. /**
  897. * lun_from_dev: - Returns the lun of the scsi device
  898. * @dev: struct scsi_device
  899. *
  900. */
  901. static inline u16 lun_from_dev(struct scsi_device *dev)
  902. {
  903. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  904. }
  905. /**
  906. * ibmvscsi_queue: - The queuecommand function of the scsi template
  907. * @cmd: struct scsi_cmnd to be executed
  908. * @done: Callback function to be called when cmd is completed
  909. */
  910. static int ibmvscsi_queuecommand_lck(struct scsi_cmnd *cmnd,
  911. void (*done) (struct scsi_cmnd *))
  912. {
  913. struct srp_cmd *srp_cmd;
  914. struct srp_event_struct *evt_struct;
  915. struct srp_indirect_buf *indirect;
  916. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  917. u16 lun = lun_from_dev(cmnd->device);
  918. u8 out_fmt, in_fmt;
  919. cmnd->result = (DID_OK << 16);
  920. evt_struct = get_event_struct(&hostdata->pool);
  921. if (!evt_struct)
  922. return SCSI_MLQUEUE_HOST_BUSY;
  923. /* Set up the actual SRP IU */
  924. srp_cmd = &evt_struct->iu.srp.cmd;
  925. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  926. srp_cmd->opcode = SRP_CMD;
  927. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  928. srp_cmd->lun = cpu_to_be64(((u64)lun) << 48);
  929. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  930. if (!firmware_has_feature(FW_FEATURE_CMO))
  931. sdev_printk(KERN_ERR, cmnd->device,
  932. "couldn't convert cmd to srp_cmd\n");
  933. free_event_struct(&hostdata->pool, evt_struct);
  934. return SCSI_MLQUEUE_HOST_BUSY;
  935. }
  936. init_event_struct(evt_struct,
  937. handle_cmd_rsp,
  938. VIOSRP_SRP_FORMAT,
  939. cmnd->request->timeout/HZ);
  940. evt_struct->cmnd = cmnd;
  941. evt_struct->cmnd_done = done;
  942. /* Fix up dma address of the buffer itself */
  943. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  944. out_fmt = srp_cmd->buf_fmt >> 4;
  945. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  946. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  947. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  948. indirect->table_desc.va == 0) {
  949. indirect->table_desc.va =
  950. cpu_to_be64(be64_to_cpu(evt_struct->crq.IU_data_ptr) +
  951. offsetof(struct srp_cmd, add_data) +
  952. offsetof(struct srp_indirect_buf, desc_list));
  953. }
  954. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  955. }
  956. static DEF_SCSI_QCMD(ibmvscsi_queuecommand)
  957. /* ------------------------------------------------------------
  958. * Routines for driver initialization
  959. */
  960. /**
  961. * map_persist_bufs: - Pre-map persistent data for adapter logins
  962. * @hostdata: ibmvscsi_host_data of host
  963. *
  964. * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
  965. * Return 1 on error, 0 on success.
  966. */
  967. static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
  968. {
  969. hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
  970. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  971. if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
  972. dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
  973. return 1;
  974. }
  975. hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
  976. &hostdata->madapter_info,
  977. sizeof(hostdata->madapter_info),
  978. DMA_BIDIRECTIONAL);
  979. if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
  980. dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
  981. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  982. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  983. return 1;
  984. }
  985. return 0;
  986. }
  987. /**
  988. * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
  989. * @hostdata: ibmvscsi_host_data of host
  990. *
  991. * Unmap the capabilities and adapter info DMA buffers
  992. */
  993. static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
  994. {
  995. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  996. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  997. dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
  998. sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
  999. }
  1000. /**
  1001. * login_rsp: - Handle response to SRP login request
  1002. * @evt_struct: srp_event_struct with the response
  1003. *
  1004. * Used as a "done" callback by when sending srp_login. Gets called
  1005. * by ibmvscsi_handle_crq()
  1006. */
  1007. static void login_rsp(struct srp_event_struct *evt_struct)
  1008. {
  1009. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1010. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  1011. case SRP_LOGIN_RSP: /* it worked! */
  1012. break;
  1013. case SRP_LOGIN_REJ: /* refused! */
  1014. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  1015. evt_struct->xfer_iu->srp.login_rej.reason);
  1016. /* Login failed. */
  1017. atomic_set(&hostdata->request_limit, -1);
  1018. return;
  1019. default:
  1020. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  1021. evt_struct->xfer_iu->srp.login_rsp.opcode);
  1022. /* Login failed. */
  1023. atomic_set(&hostdata->request_limit, -1);
  1024. return;
  1025. }
  1026. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  1027. hostdata->client_migrated = 0;
  1028. /* Now we know what the real request-limit is.
  1029. * This value is set rather than added to request_limit because
  1030. * request_limit could have been set to -1 by this client.
  1031. */
  1032. atomic_set(&hostdata->request_limit,
  1033. be32_to_cpu(evt_struct->xfer_iu->srp.login_rsp.req_lim_delta));
  1034. /* If we had any pending I/Os, kick them */
  1035. scsi_unblock_requests(hostdata->host);
  1036. }
  1037. /**
  1038. * send_srp_login: - Sends the srp login
  1039. * @hostdata: ibmvscsi_host_data of host
  1040. *
  1041. * Returns zero if successful.
  1042. */
  1043. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  1044. {
  1045. int rc;
  1046. unsigned long flags;
  1047. struct srp_login_req *login;
  1048. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  1049. BUG_ON(!evt_struct);
  1050. init_event_struct(evt_struct, login_rsp,
  1051. VIOSRP_SRP_FORMAT, login_timeout);
  1052. login = &evt_struct->iu.srp.login_req;
  1053. memset(login, 0, sizeof(*login));
  1054. login->opcode = SRP_LOGIN_REQ;
  1055. login->req_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
  1056. login->req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  1057. SRP_BUF_FORMAT_INDIRECT);
  1058. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1059. /* Start out with a request limit of 0, since this is negotiated in
  1060. * the login request we are just sending and login requests always
  1061. * get sent by the driver regardless of request_limit.
  1062. */
  1063. atomic_set(&hostdata->request_limit, 0);
  1064. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
  1065. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1066. dev_info(hostdata->dev, "sent SRP login\n");
  1067. return rc;
  1068. };
  1069. /**
  1070. * capabilities_rsp: - Handle response to MAD adapter capabilities request
  1071. * @evt_struct: srp_event_struct with the response
  1072. *
  1073. * Used as a "done" callback by when sending adapter_info.
  1074. */
  1075. static void capabilities_rsp(struct srp_event_struct *evt_struct)
  1076. {
  1077. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1078. if (evt_struct->xfer_iu->mad.capabilities.common.status) {
  1079. dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
  1080. evt_struct->xfer_iu->mad.capabilities.common.status);
  1081. } else {
  1082. if (hostdata->caps.migration.common.server_support !=
  1083. cpu_to_be16(SERVER_SUPPORTS_CAP))
  1084. dev_info(hostdata->dev, "Partition migration not supported\n");
  1085. if (client_reserve) {
  1086. if (hostdata->caps.reserve.common.server_support ==
  1087. cpu_to_be16(SERVER_SUPPORTS_CAP))
  1088. dev_info(hostdata->dev, "Client reserve enabled\n");
  1089. else
  1090. dev_info(hostdata->dev, "Client reserve not supported\n");
  1091. }
  1092. }
  1093. send_srp_login(hostdata);
  1094. }
  1095. /**
  1096. * send_mad_capabilities: - Sends the mad capabilities request
  1097. * and stores the result so it can be retrieved with
  1098. * @hostdata: ibmvscsi_host_data of host
  1099. */
  1100. static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
  1101. {
  1102. struct viosrp_capabilities *req;
  1103. struct srp_event_struct *evt_struct;
  1104. unsigned long flags;
  1105. struct device_node *of_node = hostdata->dev->of_node;
  1106. const char *location;
  1107. evt_struct = get_event_struct(&hostdata->pool);
  1108. BUG_ON(!evt_struct);
  1109. init_event_struct(evt_struct, capabilities_rsp,
  1110. VIOSRP_MAD_FORMAT, info_timeout);
  1111. req = &evt_struct->iu.mad.capabilities;
  1112. memset(req, 0, sizeof(*req));
  1113. hostdata->caps.flags = cpu_to_be32(CAP_LIST_SUPPORTED);
  1114. if (hostdata->client_migrated)
  1115. hostdata->caps.flags |= cpu_to_be32(CLIENT_MIGRATED);
  1116. strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
  1117. sizeof(hostdata->caps.name));
  1118. hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
  1119. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1120. location = location ? location : dev_name(hostdata->dev);
  1121. strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
  1122. hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
  1123. req->common.type = cpu_to_be32(VIOSRP_CAPABILITIES_TYPE);
  1124. req->buffer = cpu_to_be64(hostdata->caps_addr);
  1125. hostdata->caps.migration.common.cap_type =
  1126. cpu_to_be32(MIGRATION_CAPABILITIES);
  1127. hostdata->caps.migration.common.length =
  1128. cpu_to_be16(sizeof(hostdata->caps.migration));
  1129. hostdata->caps.migration.common.server_support =
  1130. cpu_to_be16(SERVER_SUPPORTS_CAP);
  1131. hostdata->caps.migration.ecl = cpu_to_be32(1);
  1132. if (client_reserve) {
  1133. hostdata->caps.reserve.common.cap_type =
  1134. cpu_to_be32(RESERVATION_CAPABILITIES);
  1135. hostdata->caps.reserve.common.length =
  1136. cpu_to_be16(sizeof(hostdata->caps.reserve));
  1137. hostdata->caps.reserve.common.server_support =
  1138. cpu_to_be16(SERVER_SUPPORTS_CAP);
  1139. hostdata->caps.reserve.type =
  1140. cpu_to_be32(CLIENT_RESERVE_SCSI_2);
  1141. req->common.length =
  1142. cpu_to_be16(sizeof(hostdata->caps));
  1143. } else
  1144. req->common.length = cpu_to_be16(sizeof(hostdata->caps) -
  1145. sizeof(hostdata->caps.reserve));
  1146. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1147. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  1148. dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
  1149. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1150. };
  1151. /**
  1152. * fast_fail_rsp: - Handle response to MAD enable fast fail
  1153. * @evt_struct: srp_event_struct with the response
  1154. *
  1155. * Used as a "done" callback by when sending enable fast fail. Gets called
  1156. * by ibmvscsi_handle_crq()
  1157. */
  1158. static void fast_fail_rsp(struct srp_event_struct *evt_struct)
  1159. {
  1160. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1161. u16 status = be16_to_cpu(evt_struct->xfer_iu->mad.fast_fail.common.status);
  1162. if (status == VIOSRP_MAD_NOT_SUPPORTED)
  1163. dev_err(hostdata->dev, "fast_fail not supported in server\n");
  1164. else if (status == VIOSRP_MAD_FAILED)
  1165. dev_err(hostdata->dev, "fast_fail request failed\n");
  1166. else if (status != VIOSRP_MAD_SUCCESS)
  1167. dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
  1168. send_mad_capabilities(hostdata);
  1169. }
  1170. /**
  1171. * init_host - Start host initialization
  1172. * @hostdata: ibmvscsi_host_data of host
  1173. *
  1174. * Returns zero if successful.
  1175. */
  1176. static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
  1177. {
  1178. int rc;
  1179. unsigned long flags;
  1180. struct viosrp_fast_fail *fast_fail_mad;
  1181. struct srp_event_struct *evt_struct;
  1182. if (!fast_fail) {
  1183. send_mad_capabilities(hostdata);
  1184. return 0;
  1185. }
  1186. evt_struct = get_event_struct(&hostdata->pool);
  1187. BUG_ON(!evt_struct);
  1188. init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
  1189. fast_fail_mad = &evt_struct->iu.mad.fast_fail;
  1190. memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
  1191. fast_fail_mad->common.type = cpu_to_be32(VIOSRP_ENABLE_FAST_FAIL);
  1192. fast_fail_mad->common.length = cpu_to_be16(sizeof(*fast_fail_mad));
  1193. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1194. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  1195. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1196. return rc;
  1197. }
  1198. /**
  1199. * adapter_info_rsp: - Handle response to MAD adapter info request
  1200. * @evt_struct: srp_event_struct with the response
  1201. *
  1202. * Used as a "done" callback by when sending adapter_info. Gets called
  1203. * by ibmvscsi_handle_crq()
  1204. */
  1205. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  1206. {
  1207. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1208. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  1209. dev_err(hostdata->dev, "error %d getting adapter info\n",
  1210. evt_struct->xfer_iu->mad.adapter_info.common.status);
  1211. } else {
  1212. dev_info(hostdata->dev, "host srp version: %s, "
  1213. "host partition %s (%d), OS %d, max io %u\n",
  1214. hostdata->madapter_info.srp_version,
  1215. hostdata->madapter_info.partition_name,
  1216. be32_to_cpu(hostdata->madapter_info.partition_number),
  1217. be32_to_cpu(hostdata->madapter_info.os_type),
  1218. be32_to_cpu(hostdata->madapter_info.port_max_txu[0]));
  1219. if (hostdata->madapter_info.port_max_txu[0])
  1220. hostdata->host->max_sectors =
  1221. be32_to_cpu(hostdata->madapter_info.port_max_txu[0]) >> 9;
  1222. if (be32_to_cpu(hostdata->madapter_info.os_type) == 3 &&
  1223. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  1224. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  1225. hostdata->madapter_info.srp_version);
  1226. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  1227. MAX_INDIRECT_BUFS);
  1228. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  1229. }
  1230. if (be32_to_cpu(hostdata->madapter_info.os_type) == 3) {
  1231. enable_fast_fail(hostdata);
  1232. return;
  1233. }
  1234. }
  1235. send_srp_login(hostdata);
  1236. }
  1237. /**
  1238. * send_mad_adapter_info: - Sends the mad adapter info request
  1239. * and stores the result so it can be retrieved with
  1240. * sysfs. We COULD consider causing a failure if the
  1241. * returned SRP version doesn't match ours.
  1242. * @hostdata: ibmvscsi_host_data of host
  1243. *
  1244. * Returns zero if successful.
  1245. */
  1246. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  1247. {
  1248. struct viosrp_adapter_info *req;
  1249. struct srp_event_struct *evt_struct;
  1250. unsigned long flags;
  1251. evt_struct = get_event_struct(&hostdata->pool);
  1252. BUG_ON(!evt_struct);
  1253. init_event_struct(evt_struct,
  1254. adapter_info_rsp,
  1255. VIOSRP_MAD_FORMAT,
  1256. info_timeout);
  1257. req = &evt_struct->iu.mad.adapter_info;
  1258. memset(req, 0x00, sizeof(*req));
  1259. req->common.type = cpu_to_be32(VIOSRP_ADAPTER_INFO_TYPE);
  1260. req->common.length = cpu_to_be16(sizeof(hostdata->madapter_info));
  1261. req->buffer = cpu_to_be64(hostdata->adapter_info_addr);
  1262. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1263. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  1264. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  1265. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1266. };
  1267. /**
  1268. * init_adapter: Start virtual adapter initialization sequence
  1269. *
  1270. */
  1271. static void init_adapter(struct ibmvscsi_host_data *hostdata)
  1272. {
  1273. send_mad_adapter_info(hostdata);
  1274. }
  1275. /**
  1276. * sync_completion: Signal that a synchronous command has completed
  1277. * Note that after returning from this call, the evt_struct is freed.
  1278. * the caller waiting on this completion shouldn't touch the evt_struct
  1279. * again.
  1280. */
  1281. static void sync_completion(struct srp_event_struct *evt_struct)
  1282. {
  1283. /* copy the response back */
  1284. if (evt_struct->sync_srp)
  1285. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  1286. complete(&evt_struct->comp);
  1287. }
  1288. /**
  1289. * ibmvscsi_abort: Abort a command...from scsi host template
  1290. * send this over to the server and wait synchronously for the response
  1291. */
  1292. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  1293. {
  1294. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1295. struct srp_tsk_mgmt *tsk_mgmt;
  1296. struct srp_event_struct *evt;
  1297. struct srp_event_struct *tmp_evt, *found_evt;
  1298. union viosrp_iu srp_rsp;
  1299. int rsp_rc;
  1300. unsigned long flags;
  1301. u16 lun = lun_from_dev(cmd->device);
  1302. unsigned long wait_switch = 0;
  1303. /* First, find this command in our sent list so we can figure
  1304. * out the correct tag
  1305. */
  1306. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1307. wait_switch = jiffies + (init_timeout * HZ);
  1308. do {
  1309. found_evt = NULL;
  1310. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1311. if (tmp_evt->cmnd == cmd) {
  1312. found_evt = tmp_evt;
  1313. break;
  1314. }
  1315. }
  1316. if (!found_evt) {
  1317. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1318. return SUCCESS;
  1319. }
  1320. evt = get_event_struct(&hostdata->pool);
  1321. if (evt == NULL) {
  1322. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1323. sdev_printk(KERN_ERR, cmd->device,
  1324. "failed to allocate abort event\n");
  1325. return FAILED;
  1326. }
  1327. init_event_struct(evt,
  1328. sync_completion,
  1329. VIOSRP_SRP_FORMAT,
  1330. abort_timeout);
  1331. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1332. /* Set up an abort SRP command */
  1333. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1334. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1335. tsk_mgmt->lun = cpu_to_be64(((u64) lun) << 48);
  1336. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  1337. tsk_mgmt->task_tag = (u64) found_evt;
  1338. evt->sync_srp = &srp_rsp;
  1339. init_completion(&evt->comp);
  1340. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
  1341. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1342. break;
  1343. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1344. msleep(10);
  1345. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1346. } while (time_before(jiffies, wait_switch));
  1347. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1348. if (rsp_rc != 0) {
  1349. sdev_printk(KERN_ERR, cmd->device,
  1350. "failed to send abort() event. rc=%d\n", rsp_rc);
  1351. return FAILED;
  1352. }
  1353. sdev_printk(KERN_INFO, cmd->device,
  1354. "aborting command. lun 0x%llx, tag 0x%llx\n",
  1355. (((u64) lun) << 48), (u64) found_evt);
  1356. wait_for_completion(&evt->comp);
  1357. /* make sure we got a good response */
  1358. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1359. if (printk_ratelimit())
  1360. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  1361. srp_rsp.srp.rsp.opcode);
  1362. return FAILED;
  1363. }
  1364. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1365. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1366. else
  1367. rsp_rc = srp_rsp.srp.rsp.status;
  1368. if (rsp_rc) {
  1369. if (printk_ratelimit())
  1370. sdev_printk(KERN_WARNING, cmd->device,
  1371. "abort code %d for task tag 0x%llx\n",
  1372. rsp_rc, tsk_mgmt->task_tag);
  1373. return FAILED;
  1374. }
  1375. /* Because we dropped the spinlock above, it's possible
  1376. * The event is no longer in our list. Make sure it didn't
  1377. * complete while we were aborting
  1378. */
  1379. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1380. found_evt = NULL;
  1381. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1382. if (tmp_evt->cmnd == cmd) {
  1383. found_evt = tmp_evt;
  1384. break;
  1385. }
  1386. }
  1387. if (found_evt == NULL) {
  1388. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1389. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
  1390. tsk_mgmt->task_tag);
  1391. return SUCCESS;
  1392. }
  1393. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
  1394. tsk_mgmt->task_tag);
  1395. cmd->result = (DID_ABORT << 16);
  1396. list_del(&found_evt->list);
  1397. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  1398. found_evt->hostdata->dev);
  1399. free_event_struct(&found_evt->hostdata->pool, found_evt);
  1400. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1401. atomic_inc(&hostdata->request_limit);
  1402. return SUCCESS;
  1403. }
  1404. /**
  1405. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  1406. * template send this over to the server and wait synchronously for the
  1407. * response
  1408. */
  1409. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  1410. {
  1411. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1412. struct srp_tsk_mgmt *tsk_mgmt;
  1413. struct srp_event_struct *evt;
  1414. struct srp_event_struct *tmp_evt, *pos;
  1415. union viosrp_iu srp_rsp;
  1416. int rsp_rc;
  1417. unsigned long flags;
  1418. u16 lun = lun_from_dev(cmd->device);
  1419. unsigned long wait_switch = 0;
  1420. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1421. wait_switch = jiffies + (init_timeout * HZ);
  1422. do {
  1423. evt = get_event_struct(&hostdata->pool);
  1424. if (evt == NULL) {
  1425. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1426. sdev_printk(KERN_ERR, cmd->device,
  1427. "failed to allocate reset event\n");
  1428. return FAILED;
  1429. }
  1430. init_event_struct(evt,
  1431. sync_completion,
  1432. VIOSRP_SRP_FORMAT,
  1433. reset_timeout);
  1434. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1435. /* Set up a lun reset SRP command */
  1436. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1437. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1438. tsk_mgmt->lun = cpu_to_be64(((u64) lun) << 48);
  1439. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1440. evt->sync_srp = &srp_rsp;
  1441. init_completion(&evt->comp);
  1442. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
  1443. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1444. break;
  1445. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1446. msleep(10);
  1447. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1448. } while (time_before(jiffies, wait_switch));
  1449. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1450. if (rsp_rc != 0) {
  1451. sdev_printk(KERN_ERR, cmd->device,
  1452. "failed to send reset event. rc=%d\n", rsp_rc);
  1453. return FAILED;
  1454. }
  1455. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
  1456. (((u64) lun) << 48));
  1457. wait_for_completion(&evt->comp);
  1458. /* make sure we got a good response */
  1459. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1460. if (printk_ratelimit())
  1461. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1462. srp_rsp.srp.rsp.opcode);
  1463. return FAILED;
  1464. }
  1465. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1466. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1467. else
  1468. rsp_rc = srp_rsp.srp.rsp.status;
  1469. if (rsp_rc) {
  1470. if (printk_ratelimit())
  1471. sdev_printk(KERN_WARNING, cmd->device,
  1472. "reset code %d for task tag 0x%llx\n",
  1473. rsp_rc, tsk_mgmt->task_tag);
  1474. return FAILED;
  1475. }
  1476. /* We need to find all commands for this LUN that have not yet been
  1477. * responded to, and fail them with DID_RESET
  1478. */
  1479. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1480. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1481. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1482. if (tmp_evt->cmnd)
  1483. tmp_evt->cmnd->result = (DID_RESET << 16);
  1484. list_del(&tmp_evt->list);
  1485. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1486. tmp_evt->hostdata->dev);
  1487. free_event_struct(&tmp_evt->hostdata->pool,
  1488. tmp_evt);
  1489. atomic_inc(&hostdata->request_limit);
  1490. if (tmp_evt->cmnd_done)
  1491. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1492. else if (tmp_evt->done)
  1493. tmp_evt->done(tmp_evt);
  1494. }
  1495. }
  1496. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1497. return SUCCESS;
  1498. }
  1499. /**
  1500. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1501. * @cmd: struct scsi_cmnd having problems
  1502. */
  1503. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1504. {
  1505. unsigned long wait_switch = 0;
  1506. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1507. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1508. ibmvscsi_reset_host(hostdata);
  1509. for (wait_switch = jiffies + (init_timeout * HZ);
  1510. time_before(jiffies, wait_switch) &&
  1511. atomic_read(&hostdata->request_limit) < 2;) {
  1512. msleep(10);
  1513. }
  1514. if (atomic_read(&hostdata->request_limit) <= 0)
  1515. return FAILED;
  1516. return SUCCESS;
  1517. }
  1518. /**
  1519. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1520. * @crq: Command/Response queue
  1521. * @hostdata: ibmvscsi_host_data of host
  1522. *
  1523. */
  1524. static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1525. struct ibmvscsi_host_data *hostdata)
  1526. {
  1527. long rc;
  1528. unsigned long flags;
  1529. /* The hypervisor copies our tag value here so no byteswapping */
  1530. struct srp_event_struct *evt_struct =
  1531. (__force struct srp_event_struct *)crq->IU_data_ptr;
  1532. switch (crq->valid) {
  1533. case 0xC0: /* initialization */
  1534. switch (crq->format) {
  1535. case 0x01: /* Initialization message */
  1536. dev_info(hostdata->dev, "partner initialized\n");
  1537. /* Send back a response */
  1538. rc = ibmvscsi_send_crq(hostdata, 0xC002000000000000LL, 0);
  1539. if (rc == 0) {
  1540. /* Now login */
  1541. init_adapter(hostdata);
  1542. } else {
  1543. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1544. }
  1545. break;
  1546. case 0x02: /* Initialization response */
  1547. dev_info(hostdata->dev, "partner initialization complete\n");
  1548. /* Now login */
  1549. init_adapter(hostdata);
  1550. break;
  1551. default:
  1552. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1553. }
  1554. return;
  1555. case 0xFF: /* Hypervisor telling us the connection is closed */
  1556. scsi_block_requests(hostdata->host);
  1557. atomic_set(&hostdata->request_limit, 0);
  1558. if (crq->format == 0x06) {
  1559. /* We need to re-setup the interpartition connection */
  1560. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1561. hostdata->client_migrated = 1;
  1562. hostdata->reenable_crq = 1;
  1563. purge_requests(hostdata, DID_REQUEUE);
  1564. wake_up(&hostdata->work_wait_q);
  1565. } else {
  1566. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1567. crq->format);
  1568. ibmvscsi_reset_host(hostdata);
  1569. }
  1570. return;
  1571. case 0x80: /* real payload */
  1572. break;
  1573. default:
  1574. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1575. crq->valid);
  1576. return;
  1577. }
  1578. /* The only kind of payload CRQs we should get are responses to
  1579. * things we send. Make sure this response is to something we
  1580. * actually sent
  1581. */
  1582. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1583. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1584. evt_struct);
  1585. return;
  1586. }
  1587. if (atomic_read(&evt_struct->free)) {
  1588. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1589. evt_struct);
  1590. return;
  1591. }
  1592. if (crq->format == VIOSRP_SRP_FORMAT)
  1593. atomic_add(be32_to_cpu(evt_struct->xfer_iu->srp.rsp.req_lim_delta),
  1594. &hostdata->request_limit);
  1595. del_timer(&evt_struct->timer);
  1596. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1597. evt_struct->cmnd->result = DID_ERROR << 16;
  1598. if (evt_struct->done)
  1599. evt_struct->done(evt_struct);
  1600. else
  1601. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1602. /*
  1603. * Lock the host_lock before messing with these structures, since we
  1604. * are running in a task context
  1605. */
  1606. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1607. list_del(&evt_struct->list);
  1608. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1609. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1610. }
  1611. /**
  1612. * ibmvscsi_get_host_config: Send the command to the server to get host
  1613. * configuration data. The data is opaque to us.
  1614. */
  1615. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1616. unsigned char *buffer, int length)
  1617. {
  1618. struct viosrp_host_config *host_config;
  1619. struct srp_event_struct *evt_struct;
  1620. unsigned long flags;
  1621. dma_addr_t addr;
  1622. int rc;
  1623. evt_struct = get_event_struct(&hostdata->pool);
  1624. if (!evt_struct) {
  1625. dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
  1626. return -1;
  1627. }
  1628. init_event_struct(evt_struct,
  1629. sync_completion,
  1630. VIOSRP_MAD_FORMAT,
  1631. info_timeout);
  1632. host_config = &evt_struct->iu.mad.host_config;
  1633. /* The transport length field is only 16-bit */
  1634. length = min(0xffff, length);
  1635. /* Set up a lun reset SRP command */
  1636. memset(host_config, 0x00, sizeof(*host_config));
  1637. host_config->common.type = cpu_to_be32(VIOSRP_HOST_CONFIG_TYPE);
  1638. host_config->common.length = cpu_to_be16(length);
  1639. addr = dma_map_single(hostdata->dev, buffer, length, DMA_BIDIRECTIONAL);
  1640. if (dma_mapping_error(hostdata->dev, addr)) {
  1641. if (!firmware_has_feature(FW_FEATURE_CMO))
  1642. dev_err(hostdata->dev,
  1643. "dma_mapping error getting host config\n");
  1644. free_event_struct(&hostdata->pool, evt_struct);
  1645. return -1;
  1646. }
  1647. host_config->buffer = cpu_to_be64(addr);
  1648. init_completion(&evt_struct->comp);
  1649. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1650. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  1651. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1652. if (rc == 0)
  1653. wait_for_completion(&evt_struct->comp);
  1654. dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
  1655. return rc;
  1656. }
  1657. /**
  1658. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1659. * @sdev: struct scsi_device device to configure
  1660. *
  1661. * Enable allow_restart for a device if it is a disk. Adjust the
  1662. * queue_depth here also as is required by the documentation for
  1663. * struct scsi_host_template.
  1664. */
  1665. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1666. {
  1667. struct Scsi_Host *shost = sdev->host;
  1668. unsigned long lock_flags = 0;
  1669. spin_lock_irqsave(shost->host_lock, lock_flags);
  1670. if (sdev->type == TYPE_DISK) {
  1671. sdev->allow_restart = 1;
  1672. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  1673. }
  1674. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1675. scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
  1676. return 0;
  1677. }
  1678. /**
  1679. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1680. * @sdev: scsi device struct
  1681. * @qdepth: depth to set
  1682. * @reason: calling context
  1683. *
  1684. * Return value:
  1685. * actual depth set
  1686. **/
  1687. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
  1688. int reason)
  1689. {
  1690. if (reason != SCSI_QDEPTH_DEFAULT)
  1691. return -EOPNOTSUPP;
  1692. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1693. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1694. scsi_adjust_queue_depth(sdev, 0, qdepth);
  1695. return sdev->queue_depth;
  1696. }
  1697. /* ------------------------------------------------------------
  1698. * sysfs attributes
  1699. */
  1700. static ssize_t show_host_vhost_loc(struct device *dev,
  1701. struct device_attribute *attr, char *buf)
  1702. {
  1703. struct Scsi_Host *shost = class_to_shost(dev);
  1704. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1705. int len;
  1706. len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
  1707. hostdata->caps.loc);
  1708. return len;
  1709. }
  1710. static struct device_attribute ibmvscsi_host_vhost_loc = {
  1711. .attr = {
  1712. .name = "vhost_loc",
  1713. .mode = S_IRUGO,
  1714. },
  1715. .show = show_host_vhost_loc,
  1716. };
  1717. static ssize_t show_host_vhost_name(struct device *dev,
  1718. struct device_attribute *attr, char *buf)
  1719. {
  1720. struct Scsi_Host *shost = class_to_shost(dev);
  1721. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1722. int len;
  1723. len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
  1724. hostdata->caps.name);
  1725. return len;
  1726. }
  1727. static struct device_attribute ibmvscsi_host_vhost_name = {
  1728. .attr = {
  1729. .name = "vhost_name",
  1730. .mode = S_IRUGO,
  1731. },
  1732. .show = show_host_vhost_name,
  1733. };
  1734. static ssize_t show_host_srp_version(struct device *dev,
  1735. struct device_attribute *attr, char *buf)
  1736. {
  1737. struct Scsi_Host *shost = class_to_shost(dev);
  1738. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1739. int len;
  1740. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1741. hostdata->madapter_info.srp_version);
  1742. return len;
  1743. }
  1744. static struct device_attribute ibmvscsi_host_srp_version = {
  1745. .attr = {
  1746. .name = "srp_version",
  1747. .mode = S_IRUGO,
  1748. },
  1749. .show = show_host_srp_version,
  1750. };
  1751. static ssize_t show_host_partition_name(struct device *dev,
  1752. struct device_attribute *attr,
  1753. char *buf)
  1754. {
  1755. struct Scsi_Host *shost = class_to_shost(dev);
  1756. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1757. int len;
  1758. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1759. hostdata->madapter_info.partition_name);
  1760. return len;
  1761. }
  1762. static struct device_attribute ibmvscsi_host_partition_name = {
  1763. .attr = {
  1764. .name = "partition_name",
  1765. .mode = S_IRUGO,
  1766. },
  1767. .show = show_host_partition_name,
  1768. };
  1769. static ssize_t show_host_partition_number(struct device *dev,
  1770. struct device_attribute *attr,
  1771. char *buf)
  1772. {
  1773. struct Scsi_Host *shost = class_to_shost(dev);
  1774. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1775. int len;
  1776. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1777. hostdata->madapter_info.partition_number);
  1778. return len;
  1779. }
  1780. static struct device_attribute ibmvscsi_host_partition_number = {
  1781. .attr = {
  1782. .name = "partition_number",
  1783. .mode = S_IRUGO,
  1784. },
  1785. .show = show_host_partition_number,
  1786. };
  1787. static ssize_t show_host_mad_version(struct device *dev,
  1788. struct device_attribute *attr, char *buf)
  1789. {
  1790. struct Scsi_Host *shost = class_to_shost(dev);
  1791. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1792. int len;
  1793. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1794. hostdata->madapter_info.mad_version);
  1795. return len;
  1796. }
  1797. static struct device_attribute ibmvscsi_host_mad_version = {
  1798. .attr = {
  1799. .name = "mad_version",
  1800. .mode = S_IRUGO,
  1801. },
  1802. .show = show_host_mad_version,
  1803. };
  1804. static ssize_t show_host_os_type(struct device *dev,
  1805. struct device_attribute *attr, char *buf)
  1806. {
  1807. struct Scsi_Host *shost = class_to_shost(dev);
  1808. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1809. int len;
  1810. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1811. return len;
  1812. }
  1813. static struct device_attribute ibmvscsi_host_os_type = {
  1814. .attr = {
  1815. .name = "os_type",
  1816. .mode = S_IRUGO,
  1817. },
  1818. .show = show_host_os_type,
  1819. };
  1820. static ssize_t show_host_config(struct device *dev,
  1821. struct device_attribute *attr, char *buf)
  1822. {
  1823. struct Scsi_Host *shost = class_to_shost(dev);
  1824. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1825. /* returns null-terminated host config data */
  1826. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1827. return strlen(buf);
  1828. else
  1829. return 0;
  1830. }
  1831. static struct device_attribute ibmvscsi_host_config = {
  1832. .attr = {
  1833. .name = "config",
  1834. .mode = S_IRUGO,
  1835. },
  1836. .show = show_host_config,
  1837. };
  1838. static struct device_attribute *ibmvscsi_attrs[] = {
  1839. &ibmvscsi_host_vhost_loc,
  1840. &ibmvscsi_host_vhost_name,
  1841. &ibmvscsi_host_srp_version,
  1842. &ibmvscsi_host_partition_name,
  1843. &ibmvscsi_host_partition_number,
  1844. &ibmvscsi_host_mad_version,
  1845. &ibmvscsi_host_os_type,
  1846. &ibmvscsi_host_config,
  1847. NULL
  1848. };
  1849. /* ------------------------------------------------------------
  1850. * SCSI driver registration
  1851. */
  1852. static struct scsi_host_template driver_template = {
  1853. .module = THIS_MODULE,
  1854. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1855. .proc_name = "ibmvscsi",
  1856. .queuecommand = ibmvscsi_queuecommand,
  1857. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1858. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1859. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1860. .slave_configure = ibmvscsi_slave_configure,
  1861. .change_queue_depth = ibmvscsi_change_queue_depth,
  1862. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1863. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1864. .this_id = -1,
  1865. .sg_tablesize = SG_ALL,
  1866. .use_clustering = ENABLE_CLUSTERING,
  1867. .shost_attrs = ibmvscsi_attrs,
  1868. };
  1869. /**
  1870. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1871. *
  1872. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1873. *
  1874. * Return value:
  1875. * Number of bytes of IO data the driver will need to perform well.
  1876. */
  1877. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1878. {
  1879. /* iu_storage data allocated in initialize_event_pool */
  1880. unsigned long desired_io = max_events * sizeof(union viosrp_iu);
  1881. /* add io space for sg data */
  1882. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1883. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1884. return desired_io;
  1885. }
  1886. static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
  1887. {
  1888. int rc;
  1889. char *action = "reset";
  1890. if (hostdata->reset_crq) {
  1891. smp_rmb();
  1892. hostdata->reset_crq = 0;
  1893. rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  1894. if (!rc)
  1895. rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
  1896. vio_enable_interrupts(to_vio_dev(hostdata->dev));
  1897. } else if (hostdata->reenable_crq) {
  1898. smp_rmb();
  1899. action = "enable";
  1900. rc = ibmvscsi_reenable_crq_queue(&hostdata->queue, hostdata);
  1901. hostdata->reenable_crq = 0;
  1902. if (!rc)
  1903. rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
  1904. } else
  1905. return;
  1906. if (rc) {
  1907. atomic_set(&hostdata->request_limit, -1);
  1908. dev_err(hostdata->dev, "error after %s\n", action);
  1909. }
  1910. scsi_unblock_requests(hostdata->host);
  1911. }
  1912. static int ibmvscsi_work_to_do(struct ibmvscsi_host_data *hostdata)
  1913. {
  1914. if (kthread_should_stop())
  1915. return 1;
  1916. else if (hostdata->reset_crq) {
  1917. smp_rmb();
  1918. return 1;
  1919. } else if (hostdata->reenable_crq) {
  1920. smp_rmb();
  1921. return 1;
  1922. }
  1923. return 0;
  1924. }
  1925. static int ibmvscsi_work(void *data)
  1926. {
  1927. struct ibmvscsi_host_data *hostdata = data;
  1928. int rc;
  1929. set_user_nice(current, -20);
  1930. while (1) {
  1931. rc = wait_event_interruptible(hostdata->work_wait_q,
  1932. ibmvscsi_work_to_do(hostdata));
  1933. BUG_ON(rc);
  1934. if (kthread_should_stop())
  1935. break;
  1936. ibmvscsi_do_work(hostdata);
  1937. }
  1938. return 0;
  1939. }
  1940. /**
  1941. * Called by bus code for each adapter
  1942. */
  1943. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1944. {
  1945. struct ibmvscsi_host_data *hostdata;
  1946. struct Scsi_Host *host;
  1947. struct device *dev = &vdev->dev;
  1948. struct srp_rport_identifiers ids;
  1949. struct srp_rport *rport;
  1950. unsigned long wait_switch = 0;
  1951. int rc;
  1952. dev_set_drvdata(&vdev->dev, NULL);
  1953. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1954. if (!host) {
  1955. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1956. goto scsi_host_alloc_failed;
  1957. }
  1958. host->transportt = ibmvscsi_transport_template;
  1959. hostdata = shost_priv(host);
  1960. memset(hostdata, 0x00, sizeof(*hostdata));
  1961. INIT_LIST_HEAD(&hostdata->sent);
  1962. init_waitqueue_head(&hostdata->work_wait_q);
  1963. hostdata->host = host;
  1964. hostdata->dev = dev;
  1965. atomic_set(&hostdata->request_limit, -1);
  1966. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1967. if (map_persist_bufs(hostdata)) {
  1968. dev_err(&vdev->dev, "couldn't map persistent buffers\n");
  1969. goto persist_bufs_failed;
  1970. }
  1971. hostdata->work_thread = kthread_run(ibmvscsi_work, hostdata, "%s_%d",
  1972. "ibmvscsi", host->host_no);
  1973. if (IS_ERR(hostdata->work_thread)) {
  1974. dev_err(&vdev->dev, "couldn't initialize kthread. rc=%ld\n",
  1975. PTR_ERR(hostdata->work_thread));
  1976. goto init_crq_failed;
  1977. }
  1978. rc = ibmvscsi_init_crq_queue(&hostdata->queue, hostdata, max_events);
  1979. if (rc != 0 && rc != H_RESOURCE) {
  1980. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1981. goto kill_kthread;
  1982. }
  1983. if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
  1984. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1985. goto init_pool_failed;
  1986. }
  1987. host->max_lun = 8;
  1988. host->max_id = max_id;
  1989. host->max_channel = max_channel;
  1990. host->max_cmd_len = 16;
  1991. if (scsi_add_host(hostdata->host, hostdata->dev))
  1992. goto add_host_failed;
  1993. /* we don't have a proper target_port_id so let's use the fake one */
  1994. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1995. sizeof(ids.port_id));
  1996. ids.roles = SRP_RPORT_ROLE_TARGET;
  1997. rport = srp_rport_add(host, &ids);
  1998. if (IS_ERR(rport))
  1999. goto add_srp_port_failed;
  2000. /* Try to send an initialization message. Note that this is allowed
  2001. * to fail if the other end is not acive. In that case we don't
  2002. * want to scan
  2003. */
  2004. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  2005. || rc == H_RESOURCE) {
  2006. /*
  2007. * Wait around max init_timeout secs for the adapter to finish
  2008. * initializing. When we are done initializing, we will have a
  2009. * valid request_limit. We don't want Linux scanning before
  2010. * we are ready.
  2011. */
  2012. for (wait_switch = jiffies + (init_timeout * HZ);
  2013. time_before(jiffies, wait_switch) &&
  2014. atomic_read(&hostdata->request_limit) < 2;) {
  2015. msleep(10);
  2016. }
  2017. /* if we now have a valid request_limit, initiate a scan */
  2018. if (atomic_read(&hostdata->request_limit) > 0)
  2019. scsi_scan_host(host);
  2020. }
  2021. dev_set_drvdata(&vdev->dev, hostdata);
  2022. return 0;
  2023. add_srp_port_failed:
  2024. scsi_remove_host(hostdata->host);
  2025. add_host_failed:
  2026. release_event_pool(&hostdata->pool, hostdata);
  2027. init_pool_failed:
  2028. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_events);
  2029. kill_kthread:
  2030. kthread_stop(hostdata->work_thread);
  2031. init_crq_failed:
  2032. unmap_persist_bufs(hostdata);
  2033. persist_bufs_failed:
  2034. scsi_host_put(host);
  2035. scsi_host_alloc_failed:
  2036. return -1;
  2037. }
  2038. static int ibmvscsi_remove(struct vio_dev *vdev)
  2039. {
  2040. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
  2041. unmap_persist_bufs(hostdata);
  2042. release_event_pool(&hostdata->pool, hostdata);
  2043. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  2044. max_events);
  2045. kthread_stop(hostdata->work_thread);
  2046. srp_remove_host(hostdata->host);
  2047. scsi_remove_host(hostdata->host);
  2048. scsi_host_put(hostdata->host);
  2049. return 0;
  2050. }
  2051. /**
  2052. * ibmvscsi_resume: Resume from suspend
  2053. * @dev: device struct
  2054. *
  2055. * We may have lost an interrupt across suspend/resume, so kick the
  2056. * interrupt handler
  2057. */
  2058. static int ibmvscsi_resume(struct device *dev)
  2059. {
  2060. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
  2061. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  2062. tasklet_schedule(&hostdata->srp_task);
  2063. return 0;
  2064. }
  2065. /**
  2066. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  2067. * support.
  2068. */
  2069. static struct vio_device_id ibmvscsi_device_table[] = {
  2070. {"vscsi", "IBM,v-scsi"},
  2071. { "", "" }
  2072. };
  2073. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  2074. static struct dev_pm_ops ibmvscsi_pm_ops = {
  2075. .resume = ibmvscsi_resume
  2076. };
  2077. static struct vio_driver ibmvscsi_driver = {
  2078. .id_table = ibmvscsi_device_table,
  2079. .probe = ibmvscsi_probe,
  2080. .remove = ibmvscsi_remove,
  2081. .get_desired_dma = ibmvscsi_get_desired_dma,
  2082. .name = "ibmvscsi",
  2083. .pm = &ibmvscsi_pm_ops,
  2084. };
  2085. static struct srp_function_template ibmvscsi_transport_functions = {
  2086. };
  2087. int __init ibmvscsi_module_init(void)
  2088. {
  2089. int ret;
  2090. /* Ensure we have two requests to do error recovery */
  2091. driver_template.can_queue = max_requests;
  2092. max_events = max_requests + 2;
  2093. if (!firmware_has_feature(FW_FEATURE_VIO))
  2094. return -ENODEV;
  2095. ibmvscsi_transport_template =
  2096. srp_attach_transport(&ibmvscsi_transport_functions);
  2097. if (!ibmvscsi_transport_template)
  2098. return -ENOMEM;
  2099. ret = vio_register_driver(&ibmvscsi_driver);
  2100. if (ret)
  2101. srp_release_transport(ibmvscsi_transport_template);
  2102. return ret;
  2103. }
  2104. void __exit ibmvscsi_module_exit(void)
  2105. {
  2106. vio_unregister_driver(&ibmvscsi_driver);
  2107. srp_release_transport(ibmvscsi_transport_template);
  2108. }
  2109. module_init(ibmvscsi_module_init);
  2110. module_exit(ibmvscsi_module_exit);