ibmvscsi.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  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-robbin 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. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <asm/firmware.h>
  73. #include <asm/vio.h>
  74. #include <asm/firmware.h>
  75. #include <scsi/scsi.h>
  76. #include <scsi/scsi_cmnd.h>
  77. #include <scsi/scsi_host.h>
  78. #include <scsi/scsi_device.h>
  79. #include <scsi/scsi_transport_srp.h>
  80. #include "ibmvscsi.h"
  81. /* The values below are somewhat arbitrary default values, but
  82. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  83. * Note that there are 3 bits of channel value, 6 bits of id, and
  84. * 5 bits of LUN.
  85. */
  86. static int max_id = 64;
  87. static int max_channel = 3;
  88. static int init_timeout = 5;
  89. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  90. static struct scsi_transport_template *ibmvscsi_transport_template;
  91. #define IBMVSCSI_VERSION "1.5.8"
  92. static struct ibmvscsi_ops *ibmvscsi_ops;
  93. MODULE_DESCRIPTION("IBM Virtual SCSI");
  94. MODULE_AUTHOR("Dave Boutcher");
  95. MODULE_LICENSE("GPL");
  96. MODULE_VERSION(IBMVSCSI_VERSION);
  97. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  98. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  99. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  100. MODULE_PARM_DESC(max_channel, "Largest channel value");
  101. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  102. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  103. module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
  104. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  105. /* ------------------------------------------------------------
  106. * Routines for the event pool and event structs
  107. */
  108. /**
  109. * initialize_event_pool: - Allocates and initializes the event pool for a host
  110. * @pool: event_pool to be initialized
  111. * @size: Number of events in pool
  112. * @hostdata: ibmvscsi_host_data who owns the event pool
  113. *
  114. * Returns zero on success.
  115. */
  116. static int initialize_event_pool(struct event_pool *pool,
  117. int size, struct ibmvscsi_host_data *hostdata)
  118. {
  119. int i;
  120. pool->size = size;
  121. pool->next = 0;
  122. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  123. if (!pool->events)
  124. return -ENOMEM;
  125. pool->iu_storage =
  126. dma_alloc_coherent(hostdata->dev,
  127. pool->size * sizeof(*pool->iu_storage),
  128. &pool->iu_token, 0);
  129. if (!pool->iu_storage) {
  130. kfree(pool->events);
  131. return -ENOMEM;
  132. }
  133. for (i = 0; i < pool->size; ++i) {
  134. struct srp_event_struct *evt = &pool->events[i];
  135. memset(&evt->crq, 0x00, sizeof(evt->crq));
  136. atomic_set(&evt->free, 1);
  137. evt->crq.valid = 0x80;
  138. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  139. evt->crq.IU_data_ptr = pool->iu_token +
  140. sizeof(*evt->xfer_iu) * i;
  141. evt->xfer_iu = pool->iu_storage + i;
  142. evt->hostdata = hostdata;
  143. evt->ext_list = NULL;
  144. evt->ext_list_token = 0;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * release_event_pool: - Frees memory of an event pool of a host
  150. * @pool: event_pool to be released
  151. * @hostdata: ibmvscsi_host_data who owns the even pool
  152. *
  153. * Returns zero on success.
  154. */
  155. static void release_event_pool(struct event_pool *pool,
  156. struct ibmvscsi_host_data *hostdata)
  157. {
  158. int i, in_use = 0;
  159. for (i = 0; i < pool->size; ++i) {
  160. if (atomic_read(&pool->events[i].free) != 1)
  161. ++in_use;
  162. if (pool->events[i].ext_list) {
  163. dma_free_coherent(hostdata->dev,
  164. SG_ALL * sizeof(struct srp_direct_buf),
  165. pool->events[i].ext_list,
  166. pool->events[i].ext_list_token);
  167. }
  168. }
  169. if (in_use)
  170. dev_warn(hostdata->dev, "releasing event pool with %d "
  171. "events still in use?\n", in_use);
  172. kfree(pool->events);
  173. dma_free_coherent(hostdata->dev,
  174. pool->size * sizeof(*pool->iu_storage),
  175. pool->iu_storage, pool->iu_token);
  176. }
  177. /**
  178. * valid_event_struct: - Determines if event is valid.
  179. * @pool: event_pool that contains the event
  180. * @evt: srp_event_struct to be checked for validity
  181. *
  182. * Returns zero if event is invalid, one otherwise.
  183. */
  184. static int valid_event_struct(struct event_pool *pool,
  185. struct srp_event_struct *evt)
  186. {
  187. int index = evt - pool->events;
  188. if (index < 0 || index >= pool->size) /* outside of bounds */
  189. return 0;
  190. if (evt != pool->events + index) /* unaligned */
  191. return 0;
  192. return 1;
  193. }
  194. /**
  195. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  196. * @pool: event_pool that contains the event
  197. * @evt: srp_event_struct to be modified
  198. *
  199. */
  200. static void free_event_struct(struct event_pool *pool,
  201. struct srp_event_struct *evt)
  202. {
  203. if (!valid_event_struct(pool, evt)) {
  204. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  205. "(not in pool %p)\n", evt, pool->events);
  206. return;
  207. }
  208. if (atomic_inc_return(&evt->free) != 1) {
  209. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  210. "which is not in use!\n", evt);
  211. return;
  212. }
  213. }
  214. /**
  215. * get_evt_struct: - Gets the next free event in pool
  216. * @pool: event_pool that contains the events to be searched
  217. *
  218. * Returns the next event in "free" state, and NULL if none are free.
  219. * Note that no synchronization is done here, we assume the host_lock
  220. * will syncrhonze things.
  221. */
  222. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  223. {
  224. int i;
  225. int poolsize = pool->size;
  226. int offset = pool->next;
  227. for (i = 0; i < poolsize; i++) {
  228. offset = (offset + 1) % poolsize;
  229. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  230. pool->next = offset;
  231. return &pool->events[offset];
  232. }
  233. }
  234. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  235. return NULL;
  236. }
  237. /**
  238. * init_event_struct: Initialize fields in an event struct that are always
  239. * required.
  240. * @evt: The event
  241. * @done: Routine to call when the event is responded to
  242. * @format: SRP or MAD format
  243. * @timeout: timeout value set in the CRQ
  244. */
  245. static void init_event_struct(struct srp_event_struct *evt_struct,
  246. void (*done) (struct srp_event_struct *),
  247. u8 format,
  248. int timeout)
  249. {
  250. evt_struct->cmnd = NULL;
  251. evt_struct->cmnd_done = NULL;
  252. evt_struct->sync_srp = NULL;
  253. evt_struct->crq.format = format;
  254. evt_struct->crq.timeout = timeout;
  255. evt_struct->done = done;
  256. }
  257. /* ------------------------------------------------------------
  258. * Routines for receiving SCSI responses from the hosting partition
  259. */
  260. /**
  261. * set_srp_direction: Set the fields in the srp related to data
  262. * direction and number of buffers based on the direction in
  263. * the scsi_cmnd and the number of buffers
  264. */
  265. static void set_srp_direction(struct scsi_cmnd *cmd,
  266. struct srp_cmd *srp_cmd,
  267. int numbuf)
  268. {
  269. u8 fmt;
  270. if (numbuf == 0)
  271. return;
  272. if (numbuf == 1)
  273. fmt = SRP_DATA_DESC_DIRECT;
  274. else {
  275. fmt = SRP_DATA_DESC_INDIRECT;
  276. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  277. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  278. srp_cmd->data_out_desc_cnt = numbuf;
  279. else
  280. srp_cmd->data_in_desc_cnt = numbuf;
  281. }
  282. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  283. srp_cmd->buf_fmt = fmt << 4;
  284. else
  285. srp_cmd->buf_fmt = fmt;
  286. }
  287. static void unmap_sg_list(int num_entries,
  288. struct device *dev,
  289. struct srp_direct_buf *md)
  290. {
  291. int i;
  292. for (i = 0; i < num_entries; ++i)
  293. dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
  294. }
  295. /**
  296. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  297. * @cmd: srp_cmd whose additional_data member will be unmapped
  298. * @dev: device for which the memory is mapped
  299. *
  300. */
  301. static void unmap_cmd_data(struct srp_cmd *cmd,
  302. struct srp_event_struct *evt_struct,
  303. struct device *dev)
  304. {
  305. u8 out_fmt, in_fmt;
  306. out_fmt = cmd->buf_fmt >> 4;
  307. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  308. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  309. return;
  310. else if (out_fmt == SRP_DATA_DESC_DIRECT ||
  311. in_fmt == SRP_DATA_DESC_DIRECT) {
  312. struct srp_direct_buf *data =
  313. (struct srp_direct_buf *) cmd->add_data;
  314. dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
  315. } else {
  316. struct srp_indirect_buf *indirect =
  317. (struct srp_indirect_buf *) cmd->add_data;
  318. int num_mapped = indirect->table_desc.len /
  319. sizeof(struct srp_direct_buf);
  320. if (num_mapped <= MAX_INDIRECT_BUFS) {
  321. unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
  322. return;
  323. }
  324. unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
  325. }
  326. }
  327. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  328. struct srp_direct_buf *md)
  329. {
  330. int i;
  331. struct scatterlist *sg;
  332. u64 total_length = 0;
  333. scsi_for_each_sg(cmd, sg, nseg, i) {
  334. struct srp_direct_buf *descr = md + i;
  335. descr->va = sg_dma_address(sg);
  336. descr->len = sg_dma_len(sg);
  337. descr->key = 0;
  338. total_length += sg_dma_len(sg);
  339. }
  340. return total_length;
  341. }
  342. /**
  343. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  344. * @cmd: Scsi_Cmnd with the scatterlist
  345. * @srp_cmd: srp_cmd that contains the memory descriptor
  346. * @dev: device for which to map dma memory
  347. *
  348. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  349. * Returns 1 on success.
  350. */
  351. static int map_sg_data(struct scsi_cmnd *cmd,
  352. struct srp_event_struct *evt_struct,
  353. struct srp_cmd *srp_cmd, struct device *dev)
  354. {
  355. int sg_mapped;
  356. u64 total_length = 0;
  357. struct srp_direct_buf *data =
  358. (struct srp_direct_buf *) srp_cmd->add_data;
  359. struct srp_indirect_buf *indirect =
  360. (struct srp_indirect_buf *) data;
  361. sg_mapped = scsi_dma_map(cmd);
  362. if (!sg_mapped)
  363. return 1;
  364. else if (sg_mapped < 0)
  365. return 0;
  366. set_srp_direction(cmd, srp_cmd, sg_mapped);
  367. /* special case; we can use a single direct descriptor */
  368. if (sg_mapped == 1) {
  369. map_sg_list(cmd, sg_mapped, data);
  370. return 1;
  371. }
  372. indirect->table_desc.va = 0;
  373. indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
  374. indirect->table_desc.key = 0;
  375. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  376. total_length = map_sg_list(cmd, sg_mapped,
  377. &indirect->desc_list[0]);
  378. indirect->len = total_length;
  379. return 1;
  380. }
  381. /* get indirect table */
  382. if (!evt_struct->ext_list) {
  383. evt_struct->ext_list = (struct srp_direct_buf *)
  384. dma_alloc_coherent(dev,
  385. SG_ALL * sizeof(struct srp_direct_buf),
  386. &evt_struct->ext_list_token, 0);
  387. if (!evt_struct->ext_list) {
  388. if (!firmware_has_feature(FW_FEATURE_CMO))
  389. sdev_printk(KERN_ERR, cmd->device,
  390. "Can't allocate memory "
  391. "for indirect table\n");
  392. return 0;
  393. }
  394. }
  395. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  396. indirect->len = total_length;
  397. indirect->table_desc.va = evt_struct->ext_list_token;
  398. indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
  399. memcpy(indirect->desc_list, evt_struct->ext_list,
  400. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  401. return 1;
  402. }
  403. /**
  404. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  405. * @cmd: struct scsi_cmnd with the memory to be mapped
  406. * @srp_cmd: srp_cmd that contains the memory descriptor
  407. * @dev: dma device for which to map dma memory
  408. *
  409. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  410. * Returns 1 on success.
  411. */
  412. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  413. struct srp_event_struct *evt_struct,
  414. struct srp_cmd *srp_cmd, struct device *dev)
  415. {
  416. switch (cmd->sc_data_direction) {
  417. case DMA_FROM_DEVICE:
  418. case DMA_TO_DEVICE:
  419. break;
  420. case DMA_NONE:
  421. return 1;
  422. case DMA_BIDIRECTIONAL:
  423. sdev_printk(KERN_ERR, cmd->device,
  424. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  425. return 0;
  426. default:
  427. sdev_printk(KERN_ERR, cmd->device,
  428. "Unknown data direction 0x%02x; can't map!\n",
  429. cmd->sc_data_direction);
  430. return 0;
  431. }
  432. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  433. }
  434. /**
  435. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  436. * @hostdata: the adapter
  437. */
  438. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  439. {
  440. struct srp_event_struct *tmp_evt, *pos;
  441. unsigned long flags;
  442. spin_lock_irqsave(hostdata->host->host_lock, flags);
  443. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  444. list_del(&tmp_evt->list);
  445. del_timer(&tmp_evt->timer);
  446. if (tmp_evt->cmnd) {
  447. tmp_evt->cmnd->result = (error_code << 16);
  448. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  449. tmp_evt,
  450. tmp_evt->hostdata->dev);
  451. if (tmp_evt->cmnd_done)
  452. tmp_evt->cmnd_done(tmp_evt->cmnd);
  453. } else if (tmp_evt->done)
  454. tmp_evt->done(tmp_evt);
  455. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  456. }
  457. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  458. }
  459. /**
  460. * ibmvscsi_reset_host - Reset the connection to the server
  461. * @hostdata: struct ibmvscsi_host_data to reset
  462. */
  463. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  464. {
  465. scsi_block_requests(hostdata->host);
  466. atomic_set(&hostdata->request_limit, 0);
  467. purge_requests(hostdata, DID_ERROR);
  468. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
  469. (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
  470. (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
  471. atomic_set(&hostdata->request_limit, -1);
  472. dev_err(hostdata->dev, "error after reset\n");
  473. }
  474. scsi_unblock_requests(hostdata->host);
  475. }
  476. /**
  477. * ibmvscsi_timeout - Internal command timeout handler
  478. * @evt_struct: struct srp_event_struct that timed out
  479. *
  480. * Called when an internally generated command times out
  481. */
  482. static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
  483. {
  484. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  485. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  486. evt_struct->iu.srp.cmd.opcode);
  487. ibmvscsi_reset_host(hostdata);
  488. }
  489. /* ------------------------------------------------------------
  490. * Routines for sending and receiving SRPs
  491. */
  492. /**
  493. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  494. * @evt_struct: evt_struct to be sent
  495. * @hostdata: ibmvscsi_host_data of host
  496. * @timeout: timeout in seconds - 0 means do not time command
  497. *
  498. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  499. * Note that this routine assumes that host_lock is held for synchronization
  500. */
  501. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  502. struct ibmvscsi_host_data *hostdata,
  503. unsigned long timeout)
  504. {
  505. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  506. int request_status = 0;
  507. int rc;
  508. /* If we have exhausted our request limit, just fail this request,
  509. * unless it is for a reset or abort.
  510. * Note that there are rare cases involving driver generated requests
  511. * (such as task management requests) that the mid layer may think we
  512. * can handle more requests (can_queue) when we actually can't
  513. */
  514. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  515. request_status =
  516. atomic_dec_if_positive(&hostdata->request_limit);
  517. /* If request limit was -1 when we started, it is now even
  518. * less than that
  519. */
  520. if (request_status < -1)
  521. goto send_error;
  522. /* Otherwise, we may have run out of requests. */
  523. /* If request limit was 0 when we started the adapter is in the
  524. * process of performing a login with the server adapter, or
  525. * we may have run out of requests.
  526. */
  527. else if (request_status == -1 &&
  528. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  529. goto send_busy;
  530. /* Abort and reset calls should make it through.
  531. * Nothing except abort and reset should use the last two
  532. * slots unless we had two or less to begin with.
  533. */
  534. else if (request_status < 2 &&
  535. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  536. /* In the case that we have less than two requests
  537. * available, check the server limit as a combination
  538. * of the request limit and the number of requests
  539. * in-flight (the size of the send list). If the
  540. * server limit is greater than 2, return busy so
  541. * that the last two are reserved for reset and abort.
  542. */
  543. int server_limit = request_status;
  544. struct srp_event_struct *tmp_evt;
  545. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  546. server_limit++;
  547. }
  548. if (server_limit > 2)
  549. goto send_busy;
  550. }
  551. }
  552. /* Copy the IU into the transfer area */
  553. *evt_struct->xfer_iu = evt_struct->iu;
  554. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  555. /* Add this to the sent list. We need to do this
  556. * before we actually send
  557. * in case it comes back REALLY fast
  558. */
  559. list_add_tail(&evt_struct->list, &hostdata->sent);
  560. init_timer(&evt_struct->timer);
  561. if (timeout) {
  562. evt_struct->timer.data = (unsigned long) evt_struct;
  563. evt_struct->timer.expires = jiffies + (timeout * HZ);
  564. evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
  565. add_timer(&evt_struct->timer);
  566. }
  567. if ((rc =
  568. ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  569. list_del(&evt_struct->list);
  570. del_timer(&evt_struct->timer);
  571. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  572. * Firmware will send a CRQ with a transport event (0xFF) to
  573. * tell this client what has happened to the transport. This
  574. * will be handled in ibmvscsi_handle_crq()
  575. */
  576. if (rc == H_CLOSED) {
  577. dev_warn(hostdata->dev, "send warning. "
  578. "Receive queue closed, will retry.\n");
  579. goto send_busy;
  580. }
  581. dev_err(hostdata->dev, "send error %d\n", rc);
  582. atomic_inc(&hostdata->request_limit);
  583. goto send_error;
  584. }
  585. return 0;
  586. send_busy:
  587. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  588. free_event_struct(&hostdata->pool, evt_struct);
  589. if (request_status != -1)
  590. atomic_inc(&hostdata->request_limit);
  591. return SCSI_MLQUEUE_HOST_BUSY;
  592. send_error:
  593. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  594. if (evt_struct->cmnd != NULL) {
  595. evt_struct->cmnd->result = DID_ERROR << 16;
  596. evt_struct->cmnd_done(evt_struct->cmnd);
  597. } else if (evt_struct->done)
  598. evt_struct->done(evt_struct);
  599. free_event_struct(&hostdata->pool, evt_struct);
  600. return 0;
  601. }
  602. /**
  603. * handle_cmd_rsp: - Handle responses from commands
  604. * @evt_struct: srp_event_struct to be handled
  605. *
  606. * Used as a callback by when sending scsi cmds.
  607. * Gets called by ibmvscsi_handle_crq()
  608. */
  609. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  610. {
  611. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  612. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  613. if (unlikely(rsp->opcode != SRP_RSP)) {
  614. if (printk_ratelimit())
  615. dev_warn(evt_struct->hostdata->dev,
  616. "bad SRP RSP type %d\n", rsp->opcode);
  617. }
  618. if (cmnd) {
  619. cmnd->result |= rsp->status;
  620. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  621. memcpy(cmnd->sense_buffer,
  622. rsp->data,
  623. rsp->sense_data_len);
  624. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  625. evt_struct,
  626. evt_struct->hostdata->dev);
  627. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  628. scsi_set_resid(cmnd, rsp->data_out_res_cnt);
  629. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  630. scsi_set_resid(cmnd, rsp->data_in_res_cnt);
  631. }
  632. if (evt_struct->cmnd_done)
  633. evt_struct->cmnd_done(cmnd);
  634. }
  635. /**
  636. * lun_from_dev: - Returns the lun of the scsi device
  637. * @dev: struct scsi_device
  638. *
  639. */
  640. static inline u16 lun_from_dev(struct scsi_device *dev)
  641. {
  642. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  643. }
  644. /**
  645. * ibmvscsi_queue: - The queuecommand function of the scsi template
  646. * @cmd: struct scsi_cmnd to be executed
  647. * @done: Callback function to be called when cmd is completed
  648. */
  649. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  650. void (*done) (struct scsi_cmnd *))
  651. {
  652. struct srp_cmd *srp_cmd;
  653. struct srp_event_struct *evt_struct;
  654. struct srp_indirect_buf *indirect;
  655. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  656. u16 lun = lun_from_dev(cmnd->device);
  657. u8 out_fmt, in_fmt;
  658. cmnd->result = (DID_OK << 16);
  659. evt_struct = get_event_struct(&hostdata->pool);
  660. if (!evt_struct)
  661. return SCSI_MLQUEUE_HOST_BUSY;
  662. /* Set up the actual SRP IU */
  663. srp_cmd = &evt_struct->iu.srp.cmd;
  664. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  665. srp_cmd->opcode = SRP_CMD;
  666. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  667. srp_cmd->lun = ((u64) lun) << 48;
  668. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  669. if (!firmware_has_feature(FW_FEATURE_CMO))
  670. sdev_printk(KERN_ERR, cmnd->device,
  671. "couldn't convert cmd to srp_cmd\n");
  672. free_event_struct(&hostdata->pool, evt_struct);
  673. return SCSI_MLQUEUE_HOST_BUSY;
  674. }
  675. init_event_struct(evt_struct,
  676. handle_cmd_rsp,
  677. VIOSRP_SRP_FORMAT,
  678. cmnd->request->timeout/HZ);
  679. evt_struct->cmnd = cmnd;
  680. evt_struct->cmnd_done = done;
  681. /* Fix up dma address of the buffer itself */
  682. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  683. out_fmt = srp_cmd->buf_fmt >> 4;
  684. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  685. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  686. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  687. indirect->table_desc.va == 0) {
  688. indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
  689. offsetof(struct srp_cmd, add_data) +
  690. offsetof(struct srp_indirect_buf, desc_list);
  691. }
  692. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  693. }
  694. /* ------------------------------------------------------------
  695. * Routines for driver initialization
  696. */
  697. /**
  698. * adapter_info_rsp: - Handle response to MAD adapter info request
  699. * @evt_struct: srp_event_struct with the response
  700. *
  701. * Used as a "done" callback by when sending adapter_info. Gets called
  702. * by ibmvscsi_handle_crq()
  703. */
  704. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  705. {
  706. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  707. dma_unmap_single(hostdata->dev,
  708. evt_struct->iu.mad.adapter_info.buffer,
  709. evt_struct->iu.mad.adapter_info.common.length,
  710. DMA_BIDIRECTIONAL);
  711. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  712. dev_err(hostdata->dev, "error %d getting adapter info\n",
  713. evt_struct->xfer_iu->mad.adapter_info.common.status);
  714. } else {
  715. dev_info(hostdata->dev, "host srp version: %s, "
  716. "host partition %s (%d), OS %d, max io %u\n",
  717. hostdata->madapter_info.srp_version,
  718. hostdata->madapter_info.partition_name,
  719. hostdata->madapter_info.partition_number,
  720. hostdata->madapter_info.os_type,
  721. hostdata->madapter_info.port_max_txu[0]);
  722. if (hostdata->madapter_info.port_max_txu[0])
  723. hostdata->host->max_sectors =
  724. hostdata->madapter_info.port_max_txu[0] >> 9;
  725. if (hostdata->madapter_info.os_type == 3 &&
  726. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  727. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  728. hostdata->madapter_info.srp_version);
  729. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  730. MAX_INDIRECT_BUFS);
  731. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  732. }
  733. }
  734. }
  735. /**
  736. * send_mad_adapter_info: - Sends the mad adapter info request
  737. * and stores the result so it can be retrieved with
  738. * sysfs. We COULD consider causing a failure if the
  739. * returned SRP version doesn't match ours.
  740. * @hostdata: ibmvscsi_host_data of host
  741. *
  742. * Returns zero if successful.
  743. */
  744. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  745. {
  746. struct viosrp_adapter_info *req;
  747. struct srp_event_struct *evt_struct;
  748. unsigned long flags;
  749. dma_addr_t addr;
  750. evt_struct = get_event_struct(&hostdata->pool);
  751. if (!evt_struct) {
  752. dev_err(hostdata->dev,
  753. "couldn't allocate an event for ADAPTER_INFO_REQ!\n");
  754. return;
  755. }
  756. init_event_struct(evt_struct,
  757. adapter_info_rsp,
  758. VIOSRP_MAD_FORMAT,
  759. init_timeout);
  760. req = &evt_struct->iu.mad.adapter_info;
  761. memset(req, 0x00, sizeof(*req));
  762. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  763. req->common.length = sizeof(hostdata->madapter_info);
  764. req->buffer = addr = dma_map_single(hostdata->dev,
  765. &hostdata->madapter_info,
  766. sizeof(hostdata->madapter_info),
  767. DMA_BIDIRECTIONAL);
  768. if (dma_mapping_error(hostdata->dev, req->buffer)) {
  769. if (!firmware_has_feature(FW_FEATURE_CMO))
  770. dev_err(hostdata->dev,
  771. "Unable to map request_buffer for "
  772. "adapter_info!\n");
  773. free_event_struct(&hostdata->pool, evt_struct);
  774. return;
  775. }
  776. spin_lock_irqsave(hostdata->host->host_lock, flags);
  777. if (ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2)) {
  778. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  779. dma_unmap_single(hostdata->dev,
  780. addr,
  781. sizeof(hostdata->madapter_info),
  782. DMA_BIDIRECTIONAL);
  783. }
  784. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  785. };
  786. /**
  787. * login_rsp: - Handle response to SRP login request
  788. * @evt_struct: srp_event_struct with the response
  789. *
  790. * Used as a "done" callback by when sending srp_login. Gets called
  791. * by ibmvscsi_handle_crq()
  792. */
  793. static void login_rsp(struct srp_event_struct *evt_struct)
  794. {
  795. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  796. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  797. case SRP_LOGIN_RSP: /* it worked! */
  798. break;
  799. case SRP_LOGIN_REJ: /* refused! */
  800. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  801. evt_struct->xfer_iu->srp.login_rej.reason);
  802. /* Login failed. */
  803. atomic_set(&hostdata->request_limit, -1);
  804. return;
  805. default:
  806. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  807. evt_struct->xfer_iu->srp.login_rsp.opcode);
  808. /* Login failed. */
  809. atomic_set(&hostdata->request_limit, -1);
  810. return;
  811. }
  812. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  813. if (evt_struct->xfer_iu->srp.login_rsp.req_lim_delta < 0)
  814. dev_err(hostdata->dev, "Invalid request_limit.\n");
  815. /* Now we know what the real request-limit is.
  816. * This value is set rather than added to request_limit because
  817. * request_limit could have been set to -1 by this client.
  818. */
  819. atomic_set(&hostdata->request_limit,
  820. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
  821. /* If we had any pending I/Os, kick them */
  822. scsi_unblock_requests(hostdata->host);
  823. send_mad_adapter_info(hostdata);
  824. return;
  825. }
  826. /**
  827. * send_srp_login: - Sends the srp login
  828. * @hostdata: ibmvscsi_host_data of host
  829. *
  830. * Returns zero if successful.
  831. */
  832. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  833. {
  834. int rc;
  835. unsigned long flags;
  836. struct srp_login_req *login;
  837. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  838. if (!evt_struct) {
  839. dev_err(hostdata->dev, "couldn't allocate an event for login req!\n");
  840. return FAILED;
  841. }
  842. init_event_struct(evt_struct,
  843. login_rsp,
  844. VIOSRP_SRP_FORMAT,
  845. init_timeout);
  846. login = &evt_struct->iu.srp.login_req;
  847. memset(login, 0x00, sizeof(struct srp_login_req));
  848. login->opcode = SRP_LOGIN_REQ;
  849. login->req_it_iu_len = sizeof(union srp_iu);
  850. login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
  851. spin_lock_irqsave(hostdata->host->host_lock, flags);
  852. /* Start out with a request limit of 0, since this is negotiated in
  853. * the login request we are just sending and login requests always
  854. * get sent by the driver regardless of request_limit.
  855. */
  856. atomic_set(&hostdata->request_limit, 0);
  857. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2);
  858. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  859. dev_info(hostdata->dev, "sent SRP login\n");
  860. return rc;
  861. };
  862. /**
  863. * sync_completion: Signal that a synchronous command has completed
  864. * Note that after returning from this call, the evt_struct is freed.
  865. * the caller waiting on this completion shouldn't touch the evt_struct
  866. * again.
  867. */
  868. static void sync_completion(struct srp_event_struct *evt_struct)
  869. {
  870. /* copy the response back */
  871. if (evt_struct->sync_srp)
  872. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  873. complete(&evt_struct->comp);
  874. }
  875. /**
  876. * ibmvscsi_abort: Abort a command...from scsi host template
  877. * send this over to the server and wait synchronously for the response
  878. */
  879. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  880. {
  881. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  882. struct srp_tsk_mgmt *tsk_mgmt;
  883. struct srp_event_struct *evt;
  884. struct srp_event_struct *tmp_evt, *found_evt;
  885. union viosrp_iu srp_rsp;
  886. int rsp_rc;
  887. unsigned long flags;
  888. u16 lun = lun_from_dev(cmd->device);
  889. unsigned long wait_switch = 0;
  890. /* First, find this command in our sent list so we can figure
  891. * out the correct tag
  892. */
  893. spin_lock_irqsave(hostdata->host->host_lock, flags);
  894. wait_switch = jiffies + (init_timeout * HZ);
  895. do {
  896. found_evt = NULL;
  897. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  898. if (tmp_evt->cmnd == cmd) {
  899. found_evt = tmp_evt;
  900. break;
  901. }
  902. }
  903. if (!found_evt) {
  904. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  905. return SUCCESS;
  906. }
  907. evt = get_event_struct(&hostdata->pool);
  908. if (evt == NULL) {
  909. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  910. sdev_printk(KERN_ERR, cmd->device,
  911. "failed to allocate abort event\n");
  912. return FAILED;
  913. }
  914. init_event_struct(evt,
  915. sync_completion,
  916. VIOSRP_SRP_FORMAT,
  917. init_timeout);
  918. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  919. /* Set up an abort SRP command */
  920. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  921. tsk_mgmt->opcode = SRP_TSK_MGMT;
  922. tsk_mgmt->lun = ((u64) lun) << 48;
  923. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  924. tsk_mgmt->task_tag = (u64) found_evt;
  925. evt->sync_srp = &srp_rsp;
  926. init_completion(&evt->comp);
  927. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, init_timeout * 2);
  928. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  929. break;
  930. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  931. msleep(10);
  932. spin_lock_irqsave(hostdata->host->host_lock, flags);
  933. } while (time_before(jiffies, wait_switch));
  934. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  935. if (rsp_rc != 0) {
  936. sdev_printk(KERN_ERR, cmd->device,
  937. "failed to send abort() event. rc=%d\n", rsp_rc);
  938. return FAILED;
  939. }
  940. sdev_printk(KERN_INFO, cmd->device,
  941. "aborting command. lun 0x%lx, tag 0x%lx\n",
  942. (((u64) lun) << 48), (u64) found_evt);
  943. wait_for_completion(&evt->comp);
  944. /* make sure we got a good response */
  945. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  946. if (printk_ratelimit())
  947. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  948. srp_rsp.srp.rsp.opcode);
  949. return FAILED;
  950. }
  951. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  952. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  953. else
  954. rsp_rc = srp_rsp.srp.rsp.status;
  955. if (rsp_rc) {
  956. if (printk_ratelimit())
  957. sdev_printk(KERN_WARNING, cmd->device,
  958. "abort code %d for task tag 0x%lx\n",
  959. rsp_rc, tsk_mgmt->task_tag);
  960. return FAILED;
  961. }
  962. /* Because we dropped the spinlock above, it's possible
  963. * The event is no longer in our list. Make sure it didn't
  964. * complete while we were aborting
  965. */
  966. spin_lock_irqsave(hostdata->host->host_lock, flags);
  967. found_evt = NULL;
  968. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  969. if (tmp_evt->cmnd == cmd) {
  970. found_evt = tmp_evt;
  971. break;
  972. }
  973. }
  974. if (found_evt == NULL) {
  975. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  976. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%lx completed\n",
  977. tsk_mgmt->task_tag);
  978. return SUCCESS;
  979. }
  980. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%lx\n",
  981. tsk_mgmt->task_tag);
  982. cmd->result = (DID_ABORT << 16);
  983. list_del(&found_evt->list);
  984. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  985. found_evt->hostdata->dev);
  986. free_event_struct(&found_evt->hostdata->pool, found_evt);
  987. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  988. atomic_inc(&hostdata->request_limit);
  989. return SUCCESS;
  990. }
  991. /**
  992. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  993. * template send this over to the server and wait synchronously for the
  994. * response
  995. */
  996. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  997. {
  998. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  999. struct srp_tsk_mgmt *tsk_mgmt;
  1000. struct srp_event_struct *evt;
  1001. struct srp_event_struct *tmp_evt, *pos;
  1002. union viosrp_iu srp_rsp;
  1003. int rsp_rc;
  1004. unsigned long flags;
  1005. u16 lun = lun_from_dev(cmd->device);
  1006. unsigned long wait_switch = 0;
  1007. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1008. wait_switch = jiffies + (init_timeout * HZ);
  1009. do {
  1010. evt = get_event_struct(&hostdata->pool);
  1011. if (evt == NULL) {
  1012. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1013. sdev_printk(KERN_ERR, cmd->device,
  1014. "failed to allocate reset event\n");
  1015. return FAILED;
  1016. }
  1017. init_event_struct(evt,
  1018. sync_completion,
  1019. VIOSRP_SRP_FORMAT,
  1020. init_timeout);
  1021. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1022. /* Set up a lun reset SRP command */
  1023. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1024. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1025. tsk_mgmt->lun = ((u64) lun) << 48;
  1026. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1027. evt->sync_srp = &srp_rsp;
  1028. init_completion(&evt->comp);
  1029. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, init_timeout * 2);
  1030. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1031. break;
  1032. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1033. msleep(10);
  1034. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1035. } while (time_before(jiffies, wait_switch));
  1036. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1037. if (rsp_rc != 0) {
  1038. sdev_printk(KERN_ERR, cmd->device,
  1039. "failed to send reset event. rc=%d\n", rsp_rc);
  1040. return FAILED;
  1041. }
  1042. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%lx\n",
  1043. (((u64) lun) << 48));
  1044. wait_for_completion(&evt->comp);
  1045. /* make sure we got a good response */
  1046. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1047. if (printk_ratelimit())
  1048. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1049. srp_rsp.srp.rsp.opcode);
  1050. return FAILED;
  1051. }
  1052. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1053. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1054. else
  1055. rsp_rc = srp_rsp.srp.rsp.status;
  1056. if (rsp_rc) {
  1057. if (printk_ratelimit())
  1058. sdev_printk(KERN_WARNING, cmd->device,
  1059. "reset code %d for task tag 0x%lx\n",
  1060. rsp_rc, tsk_mgmt->task_tag);
  1061. return FAILED;
  1062. }
  1063. /* We need to find all commands for this LUN that have not yet been
  1064. * responded to, and fail them with DID_RESET
  1065. */
  1066. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1067. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1068. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1069. if (tmp_evt->cmnd)
  1070. tmp_evt->cmnd->result = (DID_RESET << 16);
  1071. list_del(&tmp_evt->list);
  1072. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1073. tmp_evt->hostdata->dev);
  1074. free_event_struct(&tmp_evt->hostdata->pool,
  1075. tmp_evt);
  1076. atomic_inc(&hostdata->request_limit);
  1077. if (tmp_evt->cmnd_done)
  1078. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1079. else if (tmp_evt->done)
  1080. tmp_evt->done(tmp_evt);
  1081. }
  1082. }
  1083. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1084. return SUCCESS;
  1085. }
  1086. /**
  1087. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1088. * @cmd: struct scsi_cmnd having problems
  1089. */
  1090. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1091. {
  1092. unsigned long wait_switch = 0;
  1093. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1094. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1095. ibmvscsi_reset_host(hostdata);
  1096. for (wait_switch = jiffies + (init_timeout * HZ);
  1097. time_before(jiffies, wait_switch) &&
  1098. atomic_read(&hostdata->request_limit) < 2;) {
  1099. msleep(10);
  1100. }
  1101. if (atomic_read(&hostdata->request_limit) <= 0)
  1102. return FAILED;
  1103. return SUCCESS;
  1104. }
  1105. /**
  1106. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1107. * @crq: Command/Response queue
  1108. * @hostdata: ibmvscsi_host_data of host
  1109. *
  1110. */
  1111. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1112. struct ibmvscsi_host_data *hostdata)
  1113. {
  1114. long rc;
  1115. unsigned long flags;
  1116. struct srp_event_struct *evt_struct =
  1117. (struct srp_event_struct *)crq->IU_data_ptr;
  1118. switch (crq->valid) {
  1119. case 0xC0: /* initialization */
  1120. switch (crq->format) {
  1121. case 0x01: /* Initialization message */
  1122. dev_info(hostdata->dev, "partner initialized\n");
  1123. /* Send back a response */
  1124. if ((rc = ibmvscsi_ops->send_crq(hostdata,
  1125. 0xC002000000000000LL, 0)) == 0) {
  1126. /* Now login */
  1127. send_srp_login(hostdata);
  1128. } else {
  1129. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1130. }
  1131. break;
  1132. case 0x02: /* Initialization response */
  1133. dev_info(hostdata->dev, "partner initialization complete\n");
  1134. /* Now login */
  1135. send_srp_login(hostdata);
  1136. break;
  1137. default:
  1138. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1139. }
  1140. return;
  1141. case 0xFF: /* Hypervisor telling us the connection is closed */
  1142. scsi_block_requests(hostdata->host);
  1143. atomic_set(&hostdata->request_limit, 0);
  1144. if (crq->format == 0x06) {
  1145. /* We need to re-setup the interpartition connection */
  1146. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1147. purge_requests(hostdata, DID_REQUEUE);
  1148. if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
  1149. hostdata)) ||
  1150. (ibmvscsi_ops->send_crq(hostdata,
  1151. 0xC001000000000000LL, 0))) {
  1152. atomic_set(&hostdata->request_limit,
  1153. -1);
  1154. dev_err(hostdata->dev, "error after enable\n");
  1155. }
  1156. } else {
  1157. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1158. crq->format);
  1159. purge_requests(hostdata, DID_ERROR);
  1160. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
  1161. hostdata)) ||
  1162. (ibmvscsi_ops->send_crq(hostdata,
  1163. 0xC001000000000000LL, 0))) {
  1164. atomic_set(&hostdata->request_limit,
  1165. -1);
  1166. dev_err(hostdata->dev, "error after reset\n");
  1167. }
  1168. }
  1169. scsi_unblock_requests(hostdata->host);
  1170. return;
  1171. case 0x80: /* real payload */
  1172. break;
  1173. default:
  1174. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1175. crq->valid);
  1176. return;
  1177. }
  1178. /* The only kind of payload CRQs we should get are responses to
  1179. * things we send. Make sure this response is to something we
  1180. * actually sent
  1181. */
  1182. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1183. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1184. (void *)crq->IU_data_ptr);
  1185. return;
  1186. }
  1187. if (atomic_read(&evt_struct->free)) {
  1188. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1189. (void *)crq->IU_data_ptr);
  1190. return;
  1191. }
  1192. if (crq->format == VIOSRP_SRP_FORMAT)
  1193. atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
  1194. &hostdata->request_limit);
  1195. del_timer(&evt_struct->timer);
  1196. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1197. evt_struct->cmnd->result = DID_ERROR << 16;
  1198. if (evt_struct->done)
  1199. evt_struct->done(evt_struct);
  1200. else
  1201. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1202. /*
  1203. * Lock the host_lock before messing with these structures, since we
  1204. * are running in a task context
  1205. */
  1206. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1207. list_del(&evt_struct->list);
  1208. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1209. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1210. }
  1211. /**
  1212. * ibmvscsi_get_host_config: Send the command to the server to get host
  1213. * configuration data. The data is opaque to us.
  1214. */
  1215. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1216. unsigned char *buffer, int length)
  1217. {
  1218. struct viosrp_host_config *host_config;
  1219. struct srp_event_struct *evt_struct;
  1220. unsigned long flags;
  1221. dma_addr_t addr;
  1222. int rc;
  1223. evt_struct = get_event_struct(&hostdata->pool);
  1224. if (!evt_struct) {
  1225. dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
  1226. return -1;
  1227. }
  1228. init_event_struct(evt_struct,
  1229. sync_completion,
  1230. VIOSRP_MAD_FORMAT,
  1231. init_timeout);
  1232. host_config = &evt_struct->iu.mad.host_config;
  1233. /* Set up a lun reset SRP command */
  1234. memset(host_config, 0x00, sizeof(*host_config));
  1235. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1236. host_config->common.length = length;
  1237. host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
  1238. length,
  1239. DMA_BIDIRECTIONAL);
  1240. if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
  1241. if (!firmware_has_feature(FW_FEATURE_CMO))
  1242. dev_err(hostdata->dev,
  1243. "dma_mapping error getting host config\n");
  1244. free_event_struct(&hostdata->pool, evt_struct);
  1245. return -1;
  1246. }
  1247. init_completion(&evt_struct->comp);
  1248. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1249. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, init_timeout * 2);
  1250. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1251. if (rc == 0)
  1252. wait_for_completion(&evt_struct->comp);
  1253. dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
  1254. return rc;
  1255. }
  1256. /**
  1257. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1258. * @sdev: struct scsi_device device to configure
  1259. *
  1260. * Enable allow_restart for a device if it is a disk. Adjust the
  1261. * queue_depth here also as is required by the documentation for
  1262. * struct scsi_host_template.
  1263. */
  1264. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1265. {
  1266. struct Scsi_Host *shost = sdev->host;
  1267. unsigned long lock_flags = 0;
  1268. spin_lock_irqsave(shost->host_lock, lock_flags);
  1269. if (sdev->type == TYPE_DISK) {
  1270. sdev->allow_restart = 1;
  1271. sdev->timeout = 60 * HZ;
  1272. }
  1273. scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
  1274. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1275. return 0;
  1276. }
  1277. /**
  1278. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1279. * @sdev: scsi device struct
  1280. * @qdepth: depth to set
  1281. *
  1282. * Return value:
  1283. * actual depth set
  1284. **/
  1285. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1286. {
  1287. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1288. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1289. scsi_adjust_queue_depth(sdev, 0, qdepth);
  1290. return sdev->queue_depth;
  1291. }
  1292. /* ------------------------------------------------------------
  1293. * sysfs attributes
  1294. */
  1295. static ssize_t show_host_srp_version(struct device *dev,
  1296. struct device_attribute *attr, char *buf)
  1297. {
  1298. struct Scsi_Host *shost = class_to_shost(dev);
  1299. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1300. int len;
  1301. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1302. hostdata->madapter_info.srp_version);
  1303. return len;
  1304. }
  1305. static struct device_attribute ibmvscsi_host_srp_version = {
  1306. .attr = {
  1307. .name = "srp_version",
  1308. .mode = S_IRUGO,
  1309. },
  1310. .show = show_host_srp_version,
  1311. };
  1312. static ssize_t show_host_partition_name(struct device *dev,
  1313. struct device_attribute *attr,
  1314. char *buf)
  1315. {
  1316. struct Scsi_Host *shost = class_to_shost(dev);
  1317. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1318. int len;
  1319. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1320. hostdata->madapter_info.partition_name);
  1321. return len;
  1322. }
  1323. static struct device_attribute ibmvscsi_host_partition_name = {
  1324. .attr = {
  1325. .name = "partition_name",
  1326. .mode = S_IRUGO,
  1327. },
  1328. .show = show_host_partition_name,
  1329. };
  1330. static ssize_t show_host_partition_number(struct device *dev,
  1331. struct device_attribute *attr,
  1332. char *buf)
  1333. {
  1334. struct Scsi_Host *shost = class_to_shost(dev);
  1335. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1336. int len;
  1337. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1338. hostdata->madapter_info.partition_number);
  1339. return len;
  1340. }
  1341. static struct device_attribute ibmvscsi_host_partition_number = {
  1342. .attr = {
  1343. .name = "partition_number",
  1344. .mode = S_IRUGO,
  1345. },
  1346. .show = show_host_partition_number,
  1347. };
  1348. static ssize_t show_host_mad_version(struct device *dev,
  1349. struct device_attribute *attr, char *buf)
  1350. {
  1351. struct Scsi_Host *shost = class_to_shost(dev);
  1352. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1353. int len;
  1354. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1355. hostdata->madapter_info.mad_version);
  1356. return len;
  1357. }
  1358. static struct device_attribute ibmvscsi_host_mad_version = {
  1359. .attr = {
  1360. .name = "mad_version",
  1361. .mode = S_IRUGO,
  1362. },
  1363. .show = show_host_mad_version,
  1364. };
  1365. static ssize_t show_host_os_type(struct device *dev,
  1366. struct device_attribute *attr, char *buf)
  1367. {
  1368. struct Scsi_Host *shost = class_to_shost(dev);
  1369. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1370. int len;
  1371. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1372. return len;
  1373. }
  1374. static struct device_attribute ibmvscsi_host_os_type = {
  1375. .attr = {
  1376. .name = "os_type",
  1377. .mode = S_IRUGO,
  1378. },
  1379. .show = show_host_os_type,
  1380. };
  1381. static ssize_t show_host_config(struct device *dev,
  1382. struct device_attribute *attr, char *buf)
  1383. {
  1384. struct Scsi_Host *shost = class_to_shost(dev);
  1385. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1386. /* returns null-terminated host config data */
  1387. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1388. return strlen(buf);
  1389. else
  1390. return 0;
  1391. }
  1392. static struct device_attribute ibmvscsi_host_config = {
  1393. .attr = {
  1394. .name = "config",
  1395. .mode = S_IRUGO,
  1396. },
  1397. .show = show_host_config,
  1398. };
  1399. static struct device_attribute *ibmvscsi_attrs[] = {
  1400. &ibmvscsi_host_srp_version,
  1401. &ibmvscsi_host_partition_name,
  1402. &ibmvscsi_host_partition_number,
  1403. &ibmvscsi_host_mad_version,
  1404. &ibmvscsi_host_os_type,
  1405. &ibmvscsi_host_config,
  1406. NULL
  1407. };
  1408. /* ------------------------------------------------------------
  1409. * SCSI driver registration
  1410. */
  1411. static struct scsi_host_template driver_template = {
  1412. .module = THIS_MODULE,
  1413. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1414. .proc_name = "ibmvscsi",
  1415. .queuecommand = ibmvscsi_queuecommand,
  1416. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1417. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1418. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1419. .slave_configure = ibmvscsi_slave_configure,
  1420. .change_queue_depth = ibmvscsi_change_queue_depth,
  1421. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1422. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1423. .this_id = -1,
  1424. .sg_tablesize = SG_ALL,
  1425. .use_clustering = ENABLE_CLUSTERING,
  1426. .shost_attrs = ibmvscsi_attrs,
  1427. };
  1428. /**
  1429. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1430. *
  1431. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1432. *
  1433. * Return value:
  1434. * Number of bytes of IO data the driver will need to perform well.
  1435. */
  1436. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1437. {
  1438. /* iu_storage data allocated in initialize_event_pool */
  1439. unsigned long desired_io = max_requests * sizeof(union viosrp_iu);
  1440. /* add io space for sg data */
  1441. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1442. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1443. return desired_io;
  1444. }
  1445. /**
  1446. * Called by bus code for each adapter
  1447. */
  1448. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1449. {
  1450. struct ibmvscsi_host_data *hostdata;
  1451. struct Scsi_Host *host;
  1452. struct device *dev = &vdev->dev;
  1453. struct srp_rport_identifiers ids;
  1454. struct srp_rport *rport;
  1455. unsigned long wait_switch = 0;
  1456. int rc;
  1457. vdev->dev.driver_data = NULL;
  1458. driver_template.can_queue = max_requests;
  1459. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1460. if (!host) {
  1461. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1462. goto scsi_host_alloc_failed;
  1463. }
  1464. host->transportt = ibmvscsi_transport_template;
  1465. hostdata = shost_priv(host);
  1466. memset(hostdata, 0x00, sizeof(*hostdata));
  1467. INIT_LIST_HEAD(&hostdata->sent);
  1468. hostdata->host = host;
  1469. hostdata->dev = dev;
  1470. atomic_set(&hostdata->request_limit, -1);
  1471. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1472. rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_requests);
  1473. if (rc != 0 && rc != H_RESOURCE) {
  1474. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1475. goto init_crq_failed;
  1476. }
  1477. if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
  1478. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1479. goto init_pool_failed;
  1480. }
  1481. host->max_lun = 8;
  1482. host->max_id = max_id;
  1483. host->max_channel = max_channel;
  1484. if (scsi_add_host(hostdata->host, hostdata->dev))
  1485. goto add_host_failed;
  1486. /* we don't have a proper target_port_id so let's use the fake one */
  1487. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1488. sizeof(ids.port_id));
  1489. ids.roles = SRP_RPORT_ROLE_TARGET;
  1490. rport = srp_rport_add(host, &ids);
  1491. if (IS_ERR(rport))
  1492. goto add_srp_port_failed;
  1493. /* Try to send an initialization message. Note that this is allowed
  1494. * to fail if the other end is not acive. In that case we don't
  1495. * want to scan
  1496. */
  1497. if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1498. || rc == H_RESOURCE) {
  1499. /*
  1500. * Wait around max init_timeout secs for the adapter to finish
  1501. * initializing. When we are done initializing, we will have a
  1502. * valid request_limit. We don't want Linux scanning before
  1503. * we are ready.
  1504. */
  1505. for (wait_switch = jiffies + (init_timeout * HZ);
  1506. time_before(jiffies, wait_switch) &&
  1507. atomic_read(&hostdata->request_limit) < 2;) {
  1508. msleep(10);
  1509. }
  1510. /* if we now have a valid request_limit, initiate a scan */
  1511. if (atomic_read(&hostdata->request_limit) > 0)
  1512. scsi_scan_host(host);
  1513. }
  1514. vdev->dev.driver_data = hostdata;
  1515. return 0;
  1516. add_srp_port_failed:
  1517. scsi_remove_host(hostdata->host);
  1518. add_host_failed:
  1519. release_event_pool(&hostdata->pool, hostdata);
  1520. init_pool_failed:
  1521. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_requests);
  1522. init_crq_failed:
  1523. scsi_host_put(host);
  1524. scsi_host_alloc_failed:
  1525. return -1;
  1526. }
  1527. static int ibmvscsi_remove(struct vio_dev *vdev)
  1528. {
  1529. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1530. release_event_pool(&hostdata->pool, hostdata);
  1531. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
  1532. max_requests);
  1533. srp_remove_host(hostdata->host);
  1534. scsi_remove_host(hostdata->host);
  1535. scsi_host_put(hostdata->host);
  1536. return 0;
  1537. }
  1538. /**
  1539. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1540. * support.
  1541. */
  1542. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1543. {"vscsi", "IBM,v-scsi"},
  1544. { "", "" }
  1545. };
  1546. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1547. static struct vio_driver ibmvscsi_driver = {
  1548. .id_table = ibmvscsi_device_table,
  1549. .probe = ibmvscsi_probe,
  1550. .remove = ibmvscsi_remove,
  1551. .get_desired_dma = ibmvscsi_get_desired_dma,
  1552. .driver = {
  1553. .name = "ibmvscsi",
  1554. .owner = THIS_MODULE,
  1555. }
  1556. };
  1557. static struct srp_function_template ibmvscsi_transport_functions = {
  1558. };
  1559. int __init ibmvscsi_module_init(void)
  1560. {
  1561. int ret;
  1562. if (firmware_has_feature(FW_FEATURE_ISERIES))
  1563. ibmvscsi_ops = &iseriesvscsi_ops;
  1564. else if (firmware_has_feature(FW_FEATURE_VIO))
  1565. ibmvscsi_ops = &rpavscsi_ops;
  1566. else
  1567. return -ENODEV;
  1568. ibmvscsi_transport_template =
  1569. srp_attach_transport(&ibmvscsi_transport_functions);
  1570. if (!ibmvscsi_transport_template)
  1571. return -ENOMEM;
  1572. ret = vio_register_driver(&ibmvscsi_driver);
  1573. if (ret)
  1574. srp_release_transport(ibmvscsi_transport_template);
  1575. return ret;
  1576. }
  1577. void __exit ibmvscsi_module_exit(void)
  1578. {
  1579. vio_unregister_driver(&ibmvscsi_driver);
  1580. srp_release_transport(ibmvscsi_transport_template);
  1581. }
  1582. module_init(ibmvscsi_module_init);
  1583. module_exit(ibmvscsi_module_exit);