ibmvscsi.c 68 KB

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