zfcp_scsi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  22. #include "zfcp_ext.h"
  23. #include <asm/atomic.h>
  24. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  25. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  26. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  27. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  28. void (*done) (struct scsi_cmnd *));
  29. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  30. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  31. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  32. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  33. struct scsi_cmnd *);
  34. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
  35. unsigned int, unsigned int);
  36. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  37. struct zfcp_data zfcp_data = {
  38. .scsi_host_template = {
  39. .name = ZFCP_NAME,
  40. .module = THIS_MODULE,
  41. .proc_name = "zfcp",
  42. .slave_alloc = zfcp_scsi_slave_alloc,
  43. .slave_configure = zfcp_scsi_slave_configure,
  44. .slave_destroy = zfcp_scsi_slave_destroy,
  45. .queuecommand = zfcp_scsi_queuecommand,
  46. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  47. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  48. .eh_bus_reset_handler = zfcp_scsi_eh_host_reset_handler,
  49. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  50. .can_queue = 4096,
  51. .this_id = -1,
  52. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  53. .cmd_per_lun = 1,
  54. .use_clustering = 1,
  55. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  56. .max_sectors = ZFCP_MAX_SECTORS,
  57. },
  58. .driver_version = ZFCP_VERSION,
  59. };
  60. /* Find start of Response Information in FCP response unit*/
  61. char *
  62. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  63. {
  64. char *fcp_rsp_info_ptr;
  65. fcp_rsp_info_ptr =
  66. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  67. return fcp_rsp_info_ptr;
  68. }
  69. /* Find start of Sense Information in FCP response unit*/
  70. char *
  71. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  72. {
  73. char *fcp_sns_info_ptr;
  74. fcp_sns_info_ptr =
  75. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  76. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  77. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  78. fcp_rsp_iu->fcp_rsp_len;
  79. return fcp_sns_info_ptr;
  80. }
  81. static fcp_dl_t *
  82. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  83. {
  84. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  85. fcp_dl_t *fcp_dl_addr;
  86. fcp_dl_addr = (fcp_dl_t *)
  87. ((unsigned char *) fcp_cmd +
  88. sizeof (struct fcp_cmnd_iu) + additional_length);
  89. /*
  90. * fcp_dl_addr = start address of fcp_cmnd structure +
  91. * size of fixed part + size of dynamically sized add_dcp_cdb field
  92. * SEE FCP-2 documentation
  93. */
  94. return fcp_dl_addr;
  95. }
  96. fcp_dl_t
  97. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  98. {
  99. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  100. }
  101. void
  102. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  103. {
  104. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  105. }
  106. /*
  107. * note: it's a bit-or operation not an assignment
  108. * regarding the specified byte
  109. */
  110. static inline void
  111. set_byte(int *result, char status, char pos)
  112. {
  113. *result |= status << (pos * 8);
  114. }
  115. void
  116. set_host_byte(int *result, char status)
  117. {
  118. set_byte(result, status, 2);
  119. }
  120. void
  121. set_driver_byte(int *result, char status)
  122. {
  123. set_byte(result, status, 3);
  124. }
  125. static int
  126. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  127. {
  128. struct zfcp_adapter *adapter;
  129. struct zfcp_unit *unit;
  130. unsigned long flags;
  131. int retval = -ENXIO;
  132. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  133. if (!adapter)
  134. goto out;
  135. read_lock_irqsave(&zfcp_data.config_lock, flags);
  136. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  137. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  138. &unit->status)) {
  139. sdp->hostdata = unit;
  140. unit->device = sdp;
  141. zfcp_unit_get(unit);
  142. retval = 0;
  143. }
  144. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  145. out:
  146. return retval;
  147. }
  148. /**
  149. * zfcp_scsi_slave_destroy - called when scsi device is removed
  150. *
  151. * Remove reference to associated scsi device for an zfcp_unit.
  152. * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
  153. * or a scan for this device might have failed.
  154. */
  155. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  156. {
  157. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  158. if (unit) {
  159. zfcp_erp_wait(unit->port->adapter);
  160. wait_event(unit->scsi_scan_wq,
  161. atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
  162. &unit->status) == 0);
  163. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  164. sdpnt->hostdata = NULL;
  165. unit->device = NULL;
  166. zfcp_erp_unit_failed(unit);
  167. zfcp_unit_put(unit);
  168. } else
  169. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  170. "address %p\n", sdpnt);
  171. }
  172. /*
  173. * called from scsi midlayer to allow finetuning of a device.
  174. */
  175. static int
  176. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  177. {
  178. if (sdp->tagged_supported)
  179. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  180. else
  181. scsi_adjust_queue_depth(sdp, 0, 1);
  182. return 0;
  183. }
  184. /**
  185. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  186. * @scpnt: pointer to struct scsi_cmnd where result is set
  187. * @result: result to be set in scpnt (e.g. DID_ERROR)
  188. */
  189. static void
  190. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  191. {
  192. set_host_byte(&scpnt->result, result);
  193. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  194. zfcp_scsi_dbf_event_result("fail", 4,
  195. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  196. scpnt, NULL);
  197. /* return directly */
  198. scpnt->scsi_done(scpnt);
  199. }
  200. /**
  201. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  202. * zfcp_scsi_command_sync
  203. * @adapter: adapter where scsi command is issued
  204. * @unit: unit to which scsi command is sent
  205. * @scpnt: scsi command to be sent
  206. * @timer: timer to be started if request is successfully initiated
  207. *
  208. * Note: In scsi_done function must be set in scpnt.
  209. */
  210. int
  211. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  212. struct scsi_cmnd *scpnt, int use_timer)
  213. {
  214. int tmp;
  215. int retval;
  216. retval = 0;
  217. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  218. BUG_ON(scpnt->scsi_done == NULL);
  219. if (unlikely(NULL == unit)) {
  220. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  221. goto out;
  222. }
  223. if (unlikely(
  224. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  225. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  226. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  227. "0x%016Lx on adapter %s\n",
  228. unit->fcp_lun, unit->port->wwpn,
  229. zfcp_get_busid_by_adapter(adapter));
  230. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  231. goto out;
  232. }
  233. if (unlikely(
  234. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  235. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  236. "on port 0x%016Lx in recovery\n",
  237. zfcp_get_busid_by_unit(unit),
  238. unit->fcp_lun, unit->port->wwpn);
  239. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  240. goto out;
  241. }
  242. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
  243. ZFCP_REQ_AUTO_CLEANUP);
  244. if (unlikely(tmp < 0)) {
  245. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  246. retval = SCSI_MLQUEUE_HOST_BUSY;
  247. }
  248. out:
  249. return retval;
  250. }
  251. static void
  252. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  253. {
  254. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  255. complete(wait);
  256. }
  257. /**
  258. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  259. * @unit: unit where command is sent to
  260. * @scpnt: scsi command to be sent
  261. * @use_timer: indicates whether timer should be setup or not
  262. * Return: 0
  263. *
  264. * Errors are indicated in scpnt->result
  265. */
  266. int
  267. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  268. int use_timer)
  269. {
  270. int ret;
  271. DECLARE_COMPLETION_ONSTACK(wait);
  272. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  273. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  274. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
  275. use_timer);
  276. if (ret == 0)
  277. wait_for_completion(&wait);
  278. scpnt->SCp.ptr = NULL;
  279. return 0;
  280. }
  281. /*
  282. * function: zfcp_scsi_queuecommand
  283. *
  284. * purpose: enqueues a SCSI command to the specified target device
  285. *
  286. * returns: 0 - success, SCSI command enqueued
  287. * !0 - failure
  288. */
  289. static int
  290. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  291. void (*done) (struct scsi_cmnd *))
  292. {
  293. struct zfcp_unit *unit;
  294. struct zfcp_adapter *adapter;
  295. /* reset the status for this request */
  296. scpnt->result = 0;
  297. scpnt->host_scribble = NULL;
  298. scpnt->scsi_done = done;
  299. /*
  300. * figure out adapter and target device
  301. * (stored there by zfcp_scsi_slave_alloc)
  302. */
  303. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  304. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  305. return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
  306. }
  307. static struct zfcp_unit *
  308. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
  309. unsigned int lun)
  310. {
  311. struct zfcp_port *port;
  312. struct zfcp_unit *unit, *retval = NULL;
  313. list_for_each_entry(port, &adapter->port_list_head, list) {
  314. if (!port->rport || (id != port->rport->scsi_target_id))
  315. continue;
  316. list_for_each_entry(unit, &port->unit_list_head, list)
  317. if (lun == unit->scsi_lun) {
  318. retval = unit;
  319. goto out;
  320. }
  321. }
  322. out:
  323. return retval;
  324. }
  325. /**
  326. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  327. * @scpnt: pointer to scsi_cmnd to be aborted
  328. * Return: SUCCESS - command has been aborted and cleaned up in internal
  329. * bookkeeping, SCSI stack won't be called for aborted command
  330. * FAILED - otherwise
  331. *
  332. * We do not need to care for a SCSI command which completes normally
  333. * but late during this abort routine runs. We are allowed to return
  334. * late commands to the SCSI stack. It tracks the state of commands and
  335. * will handle late commands. (Usually, the normal completion of late
  336. * commands is ignored with respect to the running abort operation.)
  337. */
  338. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  339. {
  340. struct Scsi_Host *scsi_host;
  341. struct zfcp_adapter *adapter;
  342. struct zfcp_unit *unit;
  343. struct zfcp_fsf_req *fsf_req;
  344. unsigned long flags;
  345. unsigned long old_req_id;
  346. int retval = SUCCESS;
  347. scsi_host = scpnt->device->host;
  348. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  349. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  350. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  351. scpnt, zfcp_get_busid_by_adapter(adapter));
  352. /* avoid race condition between late normal completion and abort */
  353. write_lock_irqsave(&adapter->abort_lock, flags);
  354. /* Check whether corresponding fsf_req is still pending */
  355. spin_lock(&adapter->req_list_lock);
  356. fsf_req = zfcp_reqlist_find(adapter,
  357. (unsigned long) scpnt->host_scribble);
  358. spin_unlock(&adapter->req_list_lock);
  359. if (!fsf_req) {
  360. write_unlock_irqrestore(&adapter->abort_lock, flags);
  361. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
  362. retval = SUCCESS;
  363. goto out;
  364. }
  365. fsf_req->data = 0;
  366. fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  367. old_req_id = fsf_req->req_id;
  368. /* don't access old fsf_req after releasing the abort_lock */
  369. write_unlock_irqrestore(&adapter->abort_lock, flags);
  370. fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
  371. if (!fsf_req) {
  372. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  373. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  374. old_req_id);
  375. retval = FAILED;
  376. goto out;
  377. }
  378. __wait_event(fsf_req->completion_wq,
  379. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  380. if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  381. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
  382. retval = SUCCESS;
  383. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  384. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
  385. retval = SUCCESS;
  386. } else {
  387. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
  388. retval = FAILED;
  389. }
  390. zfcp_fsf_req_free(fsf_req);
  391. out:
  392. return retval;
  393. }
  394. static int
  395. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  396. {
  397. int retval;
  398. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  399. if (!unit) {
  400. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  401. retval = SUCCESS;
  402. goto out;
  403. }
  404. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  405. /*
  406. * If we do not know whether the unit supports 'logical unit reset'
  407. * then try 'logical unit reset' and proceed with 'target reset'
  408. * if 'logical unit reset' fails.
  409. * If the unit is known not to support 'logical unit reset' then
  410. * skip 'logical unit reset' and try 'target reset' immediately.
  411. */
  412. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  413. &unit->status)) {
  414. retval = zfcp_task_management_function(unit,
  415. FCP_LOGICAL_UNIT_RESET,
  416. scpnt);
  417. if (retval) {
  418. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  419. if (retval == -ENOTSUPP)
  420. atomic_set_mask
  421. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  422. &unit->status);
  423. /* fall through and try 'target reset' next */
  424. } else {
  425. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  426. unit);
  427. /* avoid 'target reset' */
  428. retval = SUCCESS;
  429. goto out;
  430. }
  431. }
  432. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  433. if (retval) {
  434. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  435. retval = FAILED;
  436. } else {
  437. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  438. retval = SUCCESS;
  439. }
  440. out:
  441. return retval;
  442. }
  443. static int
  444. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  445. struct scsi_cmnd *scpnt)
  446. {
  447. struct zfcp_adapter *adapter = unit->port->adapter;
  448. struct zfcp_fsf_req *fsf_req;
  449. int retval = 0;
  450. /* issue task management function */
  451. fsf_req = zfcp_fsf_send_fcp_command_task_management
  452. (adapter, unit, tm_flags, 0);
  453. if (!fsf_req) {
  454. ZFCP_LOG_INFO("error: creation of task management request "
  455. "failed for unit 0x%016Lx on port 0x%016Lx on "
  456. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  457. zfcp_get_busid_by_adapter(adapter));
  458. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  459. retval = -ENOMEM;
  460. goto out;
  461. }
  462. __wait_event(fsf_req->completion_wq,
  463. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  464. /*
  465. * check completion status of task management function
  466. */
  467. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  468. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  469. retval = -EIO;
  470. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  471. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  472. retval = -ENOTSUPP;
  473. } else
  474. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  475. zfcp_fsf_req_free(fsf_req);
  476. out:
  477. return retval;
  478. }
  479. /**
  480. * zfcp_scsi_eh_host_reset_handler - handler for host and bus reset
  481. */
  482. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  483. {
  484. struct zfcp_unit *unit;
  485. struct zfcp_adapter *adapter;
  486. unit = (struct zfcp_unit*) scpnt->device->hostdata;
  487. adapter = unit->port->adapter;
  488. ZFCP_LOG_NORMAL("host/bus reset because of problems with "
  489. "unit 0x%016Lx\n", unit->fcp_lun);
  490. zfcp_erp_adapter_reopen(adapter, 0);
  491. zfcp_erp_wait(adapter);
  492. return SUCCESS;
  493. }
  494. int
  495. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  496. {
  497. int retval = 0;
  498. static unsigned int unique_id = 0;
  499. if (adapter->scsi_host)
  500. goto out;
  501. /* register adapter as SCSI host with mid layer of SCSI stack */
  502. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  503. sizeof (struct zfcp_adapter *));
  504. if (!adapter->scsi_host) {
  505. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  506. "for adapter %s ",
  507. zfcp_get_busid_by_adapter(adapter));
  508. retval = -EIO;
  509. goto out;
  510. }
  511. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  512. /* tell the SCSI stack some characteristics of this adapter */
  513. adapter->scsi_host->max_id = 1;
  514. adapter->scsi_host->max_lun = 1;
  515. adapter->scsi_host->max_channel = 0;
  516. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  517. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  518. adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
  519. /*
  520. * save a pointer to our own adapter data structure within
  521. * hostdata field of SCSI host data structure
  522. */
  523. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  524. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  525. scsi_host_put(adapter->scsi_host);
  526. retval = -EIO;
  527. goto out;
  528. }
  529. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  530. out:
  531. return retval;
  532. }
  533. void
  534. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  535. {
  536. struct Scsi_Host *shost;
  537. struct zfcp_port *port;
  538. shost = adapter->scsi_host;
  539. if (!shost)
  540. return;
  541. read_lock_irq(&zfcp_data.config_lock);
  542. list_for_each_entry(port, &adapter->port_list_head, list)
  543. if (port->rport)
  544. port->rport = NULL;
  545. read_unlock_irq(&zfcp_data.config_lock);
  546. fc_remove_host(shost);
  547. scsi_remove_host(shost);
  548. scsi_host_put(shost);
  549. adapter->scsi_host = NULL;
  550. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  551. return;
  552. }
  553. /*
  554. * Support functions for FC transport class
  555. */
  556. static struct fc_host_statistics*
  557. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  558. {
  559. struct fc_host_statistics *fc_stats;
  560. if (!adapter->fc_stats) {
  561. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  562. if (!fc_stats)
  563. return NULL;
  564. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  565. }
  566. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  567. return adapter->fc_stats;
  568. }
  569. static void
  570. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  571. struct fsf_qtcb_bottom_port *data,
  572. struct fsf_qtcb_bottom_port *old)
  573. {
  574. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  575. old->seconds_since_last_reset;
  576. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  577. fc_stats->tx_words = data->tx_words - old->tx_words;
  578. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  579. fc_stats->rx_words = data->rx_words - old->rx_words;
  580. fc_stats->lip_count = data->lip - old->lip;
  581. fc_stats->nos_count = data->nos - old->nos;
  582. fc_stats->error_frames = data->error_frames - old->error_frames;
  583. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  584. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  585. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  586. fc_stats->loss_of_signal_count = data->loss_of_signal -
  587. old->loss_of_signal;
  588. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  589. old->psp_error_counts;
  590. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  591. old->invalid_tx_words;
  592. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  593. fc_stats->fcp_input_requests = data->input_requests -
  594. old->input_requests;
  595. fc_stats->fcp_output_requests = data->output_requests -
  596. old->output_requests;
  597. fc_stats->fcp_control_requests = data->control_requests -
  598. old->control_requests;
  599. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  600. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  601. }
  602. static void
  603. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  604. struct fsf_qtcb_bottom_port *data)
  605. {
  606. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  607. fc_stats->tx_frames = data->tx_frames;
  608. fc_stats->tx_words = data->tx_words;
  609. fc_stats->rx_frames = data->rx_frames;
  610. fc_stats->rx_words = data->rx_words;
  611. fc_stats->lip_count = data->lip;
  612. fc_stats->nos_count = data->nos;
  613. fc_stats->error_frames = data->error_frames;
  614. fc_stats->dumped_frames = data->dumped_frames;
  615. fc_stats->link_failure_count = data->link_failure;
  616. fc_stats->loss_of_sync_count = data->loss_of_sync;
  617. fc_stats->loss_of_signal_count = data->loss_of_signal;
  618. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  619. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  620. fc_stats->invalid_crc_count = data->invalid_crcs;
  621. fc_stats->fcp_input_requests = data->input_requests;
  622. fc_stats->fcp_output_requests = data->output_requests;
  623. fc_stats->fcp_control_requests = data->control_requests;
  624. fc_stats->fcp_input_megabytes = data->input_mb;
  625. fc_stats->fcp_output_megabytes = data->output_mb;
  626. }
  627. /**
  628. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  629. *
  630. * assumption: scsi_transport_fc synchronizes calls of
  631. * get_fc_host_stats and reset_fc_host_stats
  632. * (XXX to be checked otherwise introduce locking)
  633. */
  634. static struct fc_host_statistics *
  635. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  636. {
  637. struct zfcp_adapter *adapter;
  638. struct fc_host_statistics *fc_stats;
  639. struct fsf_qtcb_bottom_port *data;
  640. int ret;
  641. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  642. fc_stats = zfcp_init_fc_host_stats(adapter);
  643. if (!fc_stats)
  644. return NULL;
  645. data = kzalloc(sizeof(*data), GFP_KERNEL);
  646. if (!data)
  647. return NULL;
  648. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  649. if (ret) {
  650. kfree(data);
  651. return NULL; /* XXX return zeroed fc_stats? */
  652. }
  653. if (adapter->stats_reset &&
  654. ((jiffies/HZ - adapter->stats_reset) <
  655. data->seconds_since_last_reset)) {
  656. zfcp_adjust_fc_host_stats(fc_stats, data,
  657. adapter->stats_reset_data);
  658. } else
  659. zfcp_set_fc_host_stats(fc_stats, data);
  660. kfree(data);
  661. return fc_stats;
  662. }
  663. static void
  664. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  665. {
  666. struct zfcp_adapter *adapter;
  667. struct fsf_qtcb_bottom_port *data, *old_data;
  668. int ret;
  669. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  670. data = kzalloc(sizeof(*data), GFP_KERNEL);
  671. if (!data)
  672. return;
  673. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  674. if (ret) {
  675. kfree(data);
  676. } else {
  677. adapter->stats_reset = jiffies/HZ;
  678. old_data = adapter->stats_reset_data;
  679. adapter->stats_reset_data = data; /* finally freed in
  680. adater_dequeue */
  681. kfree(old_data);
  682. }
  683. }
  684. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  685. {
  686. rport->dev_loss_tmo = timeout;
  687. }
  688. struct fc_function_template zfcp_transport_functions = {
  689. .show_starget_port_id = 1,
  690. .show_starget_port_name = 1,
  691. .show_starget_node_name = 1,
  692. .show_rport_supported_classes = 1,
  693. .show_rport_maxframe_size = 1,
  694. .show_rport_dev_loss_tmo = 1,
  695. .show_host_node_name = 1,
  696. .show_host_port_name = 1,
  697. .show_host_permanent_port_name = 1,
  698. .show_host_supported_classes = 1,
  699. .show_host_supported_speeds = 1,
  700. .show_host_maxframe_size = 1,
  701. .show_host_serial_number = 1,
  702. .get_fc_host_stats = zfcp_get_fc_host_stats,
  703. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  704. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  705. /* no functions registered for following dynamic attributes but
  706. directly set by LLDD */
  707. .show_host_port_type = 1,
  708. .show_host_speed = 1,
  709. .show_host_port_id = 1,
  710. .disable_target_scan = 1,
  711. };
  712. /**
  713. * ZFCP_DEFINE_SCSI_ATTR
  714. * @_name: name of show attribute
  715. * @_format: format string
  716. * @_value: value to print
  717. *
  718. * Generates attribute for a unit.
  719. */
  720. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  721. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  722. char *buf) \
  723. { \
  724. struct scsi_device *sdev; \
  725. struct zfcp_unit *unit; \
  726. \
  727. sdev = to_scsi_device(dev); \
  728. unit = sdev->hostdata; \
  729. return sprintf(buf, _format, _value); \
  730. } \
  731. \
  732. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  733. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  734. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  735. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  736. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  737. &dev_attr_fcp_lun,
  738. &dev_attr_wwpn,
  739. &dev_attr_hba_id,
  740. NULL
  741. };
  742. #undef ZFCP_LOG_AREA