ibmvscsi.c 45 KB

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