ibmvscsi.c 70 KB

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