ibmvscsi.c 46 KB

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