ibmvscsi.c 69 KB

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